How to turn one column of a table into a csv string in SQL Server without using a cursor

前端 未结 5 1682
灰色年华
灰色年华 2020-12-14 18:02

I want to return the results of select Column from Table into a comma separated string using SQL Server.

The column in question is rather large (n

5条回答
  •  温柔的废话
    2020-12-14 18:29

    STRING_AGG was added in sql 2017

    https://docs.microsoft.com/en-us/sql/t-sql/functions/string-agg-transact-sql?view=sql-server-2017

    SELECT STRING_AGG (ColumnName, ',') AS csv 
    FROM TableName
    GROUP BY ColumnName2
    

提交回复
热议问题