Grouping columns

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • hartley_aaron@hotmail.com

    Grouping columns

    Hi,

    I was trying to retrieve some data in such a way that it 2 columns will
    be merged into one, with a column in between. I am trying to do
    something like this:

    SELECT LastName + ", " + FirstName AS Name
    FROM EmployeeTBL
    ORDER BY LastName

    But SQL Server does not like this syntax (though it does work with
    "LastName + FirstName").

    I appreciate any help.

    Thanks,
    Aaron

  • Thomas R. Hummel

    #2
    Re: Grouping columns

    SQL Server uses single quotes for strings, not double quotes. Also...
    you probably want to order by the first name if the last name is the
    same, correct? Try:

    SELECT LastName + ', ' + FirstName AS Name
    FROM EmployeeTBL
    ORDER BY LastName, FirstName

    If it is possible for there to be NULL values or empty strings in
    either of the columns then you will need to account for that as well.

    HTH,
    -Tom.

    Comment

    • SQL_developer

      #3
      Re: Grouping columns

      SELECT LastName + ", " + FirstName AS Name
      FROM EmployeeTBL
      ORDER BY Name

      This should work.

      Comment

      • boblotz2001@yahoo.com

        #4
        Re: Grouping columns

        Use single qutes instead of double:

        SELECT LastName + ', ' + FirstName AS Name
        FROM EmployeeTBL
        ORDER BY LastName

        Comment

        • SQL_developer

          #5
          Re: Grouping columns

          Hmm, I didn't notice the double quotes ealier.

          SELECT LastName + ', ' + FirstName AS Name
          FROM EmployeeTBL
          ORDER BY Name

          You can always use the final column name in the ORDER BY condition.

          Comment

          Working...