SQL Server Performance

Convert all rows into a single column output

Discussion in 'T-SQL Performance Tuning for Developers' started by nbkr3bi, May 9, 2007.

  1. nbkr3bi New Member

    I have a table Numbers:
    Nos
    123
    456
    789


    I need a query to display the output as : 123,456,789

    How can this be done?
  2. dineshasanka Moderator

    declare @str as varchar(4000)

    Set @str =''
    Select @str = cast(nos as varcahr(10)) + ',' from TableName
    Print @str

    ----------------------------------------
    Contributing Editor, Writer & Forums Moderator
    http://www.SQL-Server-Performance.Com

    Visit my Blog at
    http://dineshasanka.spaces.live.com/
  3. nbkr3bi New Member

    This doesnt work : This just displays the last row with a comma
    789,

    what i need is all the rows : 123,456,789
  4. dineshasanka Moderator

    small change need

    declare @str as varchar(4000)

    Set @str =''
    Select @str = @str + cast(nos as varcahr(10)) + ',' from TableName
    Print @str

    Sorry about it

    ----------------------------------------
    Contributing Editor, Writer & Forums Moderator
    http://www.SQL-Server-Performance.Com

    Visit my Blog at
    http://dineshasanka.spaces.live.com/
  5. Madhivanan Moderator

    Note that if the concatenated strings exceed more than 8000 characters, you need to use more than one variable

    Madhivanan

    Failing to plan is Planning to fail
  6. nbkr3bi New Member

    Yup. Thanks a lot for your help.

Share This Page