results on one line

  • Is it possible to place all the results of a select statement into one line,

    eg Select surname from stafflist

    and the results would appear like : xyz,abc,stu.....

    The table I am using is dymanic and I can not think of a simple solution to do the above query

  • ya, its possible

    DECLARE @StaffList varchar(100)

    SELECT @StaffList = COALESCE(@StaffList + ', ', '') + Surname

    FROM StaffList order by Surname asc

    SELECT @StaffList

     

    When @StaffList is NULL (the first row processed), it returns an empty string. On subsequent rows, it concatenates the @StaffList value with a comma and the current Surname.

  • Thanks a million, that worked

  • Its ok.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply