concatenate values of a column into 1 variable

  • I cant for the life of me remember how to do this...

    select surname ||","|| firstname as name from CS_STAFF

    the above doesnt work, does neone know the proper way? Thanks!

  • Is this what you are looking for:

    DECLARE @names varchar(500)

    SET @names = ''

    SELECT @names = @names + ',' + surname

    FROM CS_STAFF

    SET @names = RIGHT(@names, LEN(@names) - 1)

    PRINT @names

    Robert Marda

    SQL Server will deliver its data any way you want it

    when you give your SQL Programmer enough developing time.

    Robert W. Marda
    Billing and OSS Specialist - SQL Programmer
    MCL Systems

  • ohh what a dummy! I realise my mistake..instead of " i should have used 'so that is looks like the following:

    select SURNAME || ', ' || FIRSTNAME from CS_STAFF

    thanks for ur reply

  • or

    select SURNAME || ', ' || FIRSTNAME as Name from CS_STAFF

    if u want to give the column a name called "Name"

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

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