March 22, 2012 at 5:54 pm
How do you join two fields in the one column from the same table ?
Like FirstName and LastName to show together in one column.
Thanks.
March 22, 2012 at 5:58 pm
No Need to join.. Just the concatenation operator "+" to concatenate them
Like
Select FirstName + LastName AS FullName From Table
March 22, 2012 at 6:03 pm
Or if you want the names separated by a space or comma:
Select FirstName + ' ' + LastName AS FullName From Table
Select LastName + ', ' + FirstName AS FullName From Table
March 22, 2012 at 6:13 pm
Pwoah! Thanks guys!
Is there such thing as adding LastName in brackets?
March 22, 2012 at 6:17 pm
Yep
Select '(' + LastName + ') , ' + FirstName AS FullName From Table
Select FirstName + ' (' + LastName + ')' AS FullName From Table
March 22, 2012 at 6:19 pm
Much Appreciated all ! Thanks.
🙂
April 20, 2012 at 2:04 am
I'd suggest wrapping the column names in COALESCE in the event of a NULL value.
Select LTRIM(COALESCE(FirstName,'') + ' (' + COALESCE(LastName,'') + ')') AS FullName From MyTable
Check your database options for CONCATENATE NULL YIELD NULLS.
:exclamation: "Be brave. Take risks. Nothing can substitute experience." :exclamation:
April 20, 2012 at 4:05 am
Robin Sasson (4/20/2012)
I'd suggest wrapping the column names in COALESCE in the event of a NULL value.Select LTRIM(COALESCE(FirstName,'') + ' (' + COALESCE(LastName,'') + ')') AS FullName From MyTable
Check your database options for CONCATENATE NULL YIELD NULLS.
Just an interesting thing to note that with Sql Server 2012 (I know its a 2008 forum) you'll be able to
skip the Coalesce and the Set Statement by simply using the CONCAT Function which will convert NULL values passed to it to empty Stings.
April 20, 2012 at 4:24 am
Wow - learn something new every day on this site. Fantastic!
:exclamation: "Be brave. Take risks. Nothing can substitute experience." :exclamation:
April 20, 2012 at 4:41 am
Yep. I learnt that one yesterday 😛
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply