DECLARE @tb table ([A] int,[B] nvarchar(1)) Insert @tb select 1,N'a' union all select 2,N'b' union all select 3,N'c' Select distinct stuff((select ','+[B] from @tb for xml path('')),1,'') AS joinstr from tb t --删除指定长度的字符, 并在指定的起点处插入另一组字符 --原串: ',a,b,c' --从第1个字符','开始,删除开始的1个字符',',并在删除起始位置插入'' --joinstr --------- --a,c
更简单的:
DECLARE @tb table ([A] int,[B] nvarchar(1)) --declare @str nvarchar(max) Insert @tb select 1,N'a' union all select 2,N'b' union all select 3,N'c' Select stuff((select ','') --Select @str=stuff((select ','') --select @str