Using COALESCE function to make values separated with commas

后端 未结 4 1298
走了就别回头了
走了就别回头了 2020-11-28 08:14

I have a table (EMP) I know that using the COALESCE function we can get the values of any column in this way

23,23,45,34

SELECT          


        
4条回答
  •  佛祖请我去吃肉
    2020-11-28 08:47

    Description

    I have done this using COALESCE in the past too, but i suggest another approach because you dont need a variable. Use the T-SQL function STUFF to get this done.

    Sample

    SELECT STUFF((
        select ','+ cast(OfferID as nvarchar(255)) 
        from Emp b
        WHERE a.EmpID= b.EmpID
        FOR XML PATH('')
        ),1,1,'') AS COLUMN2
    FROM Emp a
    GROUP BY a.EmpID
    

    More Information

    STUFF (Transact-SQL)

提交回复
热议问题