Show to fields in the one column

  • 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.

  • No Need to join.. Just the concatenation operator "+" to concatenate them

    Like

    Select FirstName + LastName AS FullName From Table

  • 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

    [font="Courier New"]Looking for a Deadlock Victim Support Group..[/font]
  • Pwoah! Thanks guys!

    Is there such thing as adding LastName in brackets?

  • Yep

    Select '(' + LastName + ') , ' + FirstName AS FullName From Table

    Select FirstName + ' (' + LastName + ')' AS FullName From Table

  • Much Appreciated all ! Thanks.

    🙂

  • 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:

  • 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.

    http://msdn.microsoft.com/en-us/library/hh231515.aspx

  • Wow - learn something new every day on this site. Fantastic!

    :exclamation: "Be brave. Take risks. Nothing can substitute experience." :exclamation:

  • 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