You must Sign In to post a response.
  • Category: SQL Server

    How to return multiple items with comma separated in sql


    Are you looking for a way to return multiple items with comma separated values ? want to do it in sql ? then read this thread to know more about it



    Hi all

    Select * from table_details returns


    Code Salesman date status

    1 s1 1/1/2014 Y
    2 s2 2/1/2014 Y
    2 s1 2/1/2014 Y
    3 s2 3/1/2014 Y


    i want query to return rows like

    Code Salesman date status

    1 s1 1/1/2014 Y
    2 s2,s1 2/1/2014 Y
    3 s2 3/1/2014 Y

    How to do this

    Thanks in Advance

    Amritha
  • #748598
    Hi Amritha,

    If you want to return select statement with comma separator then check the below sample query.

    Using Concatenating you can achieve this.


    select col1 + ', '+ col2
    from tablename


    Use above format to separate the columns with comma separator.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #748616
    Hi use below script

    DECLARE @Table1 TABLE(Code INT, Salesman varchar(10),[date] date, [status] varchar(10))
    INSERT INTO @Table1 VALUES (1,'s1','1/1/2014','Y'),(2,'s2','2/1/2014','Y'),(2,'s1','2/1/2014','Y'),(3,'s2','3/1/2014','Y')


    SELECT Code
    ,STUFF((SELECT ', ' + CAST(Salesman AS VARCHAR(10)) [text()]
    FROM @Table1
    WHERE Code = t.Code
    FOR XML PATH(''), TYPE)
    .value('.','NVARCHAR(MAX)'),1,2,' ') Salesman,[date],[status]
    FROM @Table1 t
    GROUP BY Code,[date],[status]

  • #748623
    SELECT Code,status,
    STUFF((
    select','+ cast(Salesman as nvarchar(255))
    from table_details b
    WHERE a.Code= b.Code
    FOR XML PATH('')
    )
    ,1,1,'') AS Salesman
    FROM table_details a
    GROUP BY a.Code,status

    try this


  • Sign In to post your comments