SQL Server Performance

SQL convert 1 column's values to 1 row string

Discussion in 'SQL Server 2005 T-SQL Performance Tuning' started by Ismailc, Apr 18, 2009.

  1. Ismailc New Member

    Hi, I need help please.
    I have one column whith different values depending on result.
    I would like to return a one row string result with a comma seperating the values.
    The column output as follows:
    Column A
    --------
    1
    2
    3
    I would like to output as 1, 2, 3
    Please Assist.
  2. MohammedU New Member

  3. Ismailc New Member

    Thank You for the help, unfortunately not really the what i want.
    I have a query that returns different values & diffrent amount of rows depending on the user logged in.
    The column is a text/string column but i can't use a case statement.
    Regards
  4. Ismailc New Member

    Thank You, found a solution:
    DECLARE @Str varchar(500)
    SELECT @Str=COALESCE(@Str,'') + CAST(ID as varchar(10)) + ','
    FROM dbo.fcUser
    SELECT @Str
  5. Madhivanan Moderator

    [quote user="Ismailc"]
    Thank You, found a solution:
    DECLARE @Str varchar(500)
    SELECT @Str=COALESCE(@Str,'') + CAST(ID as varchar(10)) + ','
    FROM dbo.fcUser
    SELECT @Str
    [/quote]
    You may need to increase the with of @str if there are many rows in the table

Share This Page