列值转换为逗号分隔字符串

将数据表的某一列值,转换为逗号分隔字符串:

先准备一些数据:

 

DECLARE @t AS TABLE([Datas] NVARCHAR(40))
INSERT INTO @t ([Datas]) VALUES(N'DF'),(N'W4F'),(N'EYY'),(N'ER'),(N'GFF'),(N'A445')

SELECT [Datas] FROM @t ORDER BY [Datas]
Source Code

 

一二句SQL代码的事:

 

DECLARE @CommaDelimitedString NVARCHAR(MAX)
SELECT @CommaDelimitedString = ISNULL(@CommaDelimitedString + ',', '') + [Datas] FROM @t ORDER BY [Datas]
SELECT @CommaDelimitedString
Source Code

 

posted @ 2019-05-12 17:23  Insus.NET  阅读(764)  评论(0编辑  收藏  举报