简体   繁体   English

使用SQL Server将多行转换为单列

[英]Multiple Rows to single column using SQL Server

I want to convert my found data to a single column 我想将找到的数据转换为单列

My data is 我的数据是

Id
------
3
4
5

that record I want to this format 该记录我想要这种格式

id
=======
3,4,5

Try, 尝试,

 SELECT STUFF( (SELECT ',' + Id
                                 FROM My_Table
                                 ORDER BY Id
                                 FOR XML PATH('')), 
                                1, 1, '')

Original_Post_Here Original_Post_Here

Another way 其他方式

DECLARE @str VARCHAR(1000)

SELECT @str = coalesce(@str + ',', '') + a.[Your Column]
FROM (
    SELECT DISTINCT [Your Column]
    FROM [your Table]
    ) a

SELECT @str

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM