欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-03-20 19:24:00
将数据表的某一列值,转换为逗号分隔字符串: 先准备一些数据: DECLARE @t AS TABLE([Datas] NVARCHAR(40)) INSERT INTO @t ([Datas]) VALUES(N'DF'),(N'W4F'),(N'EYY'),(N'ER'),(N'GFF'),(N' ......

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

先准备一些数据:

 

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]

 

一二句sql代码的事:

 

declare @commadelimitedstring nvarchar(max)
select @commadelimitedstring = isnull(@commadelimitedstring + ',', '') + [datas] from @t order by [datas]
select @commadelimitedstring