SQL Server用SPLIT函数分割字符串
declare @str varchar(100),@sql varchar(1000)
set @str='1,2,3,4,5,6,7,8,9,10'
set @sql='select Value='''+ replace(@str,',',''' union all select ''')+''''
PRINT @sql
exec (@sql)
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_splitSTR]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[f_splitSTR]
GO
循环截取法
CREATE FUNCTION f_splitSTR(
@s varchar(8000), --待分拆的字符串
@split varchar(10) --数据分隔符
)RETURNS @re TABLE(col varchar(100))
AS
BEGIN
DECLARE @splitlen int
SET @splitlen=LEN(@split+'a')-2
WHILE CHARINDEX(@split,@s)>0
BEGIN
INSERT @re VALUES(LEFT(@s,CHARINDEX(@split,@s)-1))
SET @s=STUFF(@s,1,CHARINDEX(@split,@s)+@splitlen,'')
END
INSERT @re VALUES(@s)
RETURN
END
GO
SELECT STUFF((SELECT ','+列名 FROM 表名 WHERE 条件='xxx' for xml path('')),1,1,'') --列转为字符串,逗号分隔
注:来源于网络收集 做的笔记