SQL Server 分割字符串和合并多条数据为一行

分割字符串函数

create function f_split(@c varchar(2000),@split varchar(2))
returns @t table(col varchar(20))
as
begin

while(charindex(@split,@c)<>0)
begin
insert @t(col) values (substring(@c,1,charindex(@split,@c)-1))
set @c = stuff(@c,1,charindex(@split,@c),'')
end
insert @t(col) values (@c)
return
end
go

select * from dbo.f_split('dfkd,dfdkdf,dfdkf,dffjk',',')

源博客地址:https://www.cnblogs.com/vaevvaev/p/7018716.html

合并多行数据某列为一列:
select  [value] = stuff((select ','+modmb003  from modmb detail where modmb=18 for xml path('')), 1, 1, '')


源博客地址:https://www.cnblogs.com/vaevvaev/p/7018716.html
使用实例(若看不懂可联系QQ群644147671)

 



posted @ 2018-10-17 20:00  Zero77  阅读(638)  评论(0编辑  收藏  举报