March 28, 2014 at 2:47 pm
I have four fields:
FirstName varchar(20),null
MiddleName varchar(5),null
LastName varchar(30),null
Position int,null
How to concatenate all these four fields to give result as Name?
I tried this but it is not working properly.
select
LTRIM(RTRIM(isnull(Person.Lastname,' ')+','+isnull(Person.Firstname,' ')+' '+ISNULL(Person.middlename,' ')))+' '+CAST(person.Position as varchar) as Name
from dbo.Person
Any suggestions?
March 28, 2014 at 3:12 pm
Pritz (3/28/2014)
I have four fields:FirstName varchar(20),null
MiddleName varchar(5),null
LastName varchar(30),null
Position int,null
How to concatenate all these four fields to give result as Name?
I tried this but it is not working properly.
select
LTRIM(RTRIM(isnull(Person.Lastname,' ')+','+isnull(Person.Firstname,' ')+' '+ISNULL(Person.middlename,' ')))+' '+CAST(person.Position as varchar) as Name
from dbo.Person
Any suggestions?
I am guessing you are getting NULL when Position IS NULL? All you need to do is wrap your Position cast inside an ISNULL like the other columns.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy