sql 拆分 逗号 函数

CREATE FUNCTION [dbo].[f_splitSTR](
@string VARCHAR(MAX),   --待分拆的字符串
@splitChar VARCHAR(10)     --数据分隔符
)RETURNS @result TABLE(col VARCHAR(100))
AS
BEGIN
DECLARE @splitLen INT
SET @splitLen=LEN(@splitChar)-1
WHILE CHARINDEX(@splitChar,@string)>0
BEGIN
    INSERT @result VALUES(LEFT(@string,CHARINDEX(@splitChar,@string)-1))
    SET @string=STUFF(@string,1,CHARINDEX(@splitChar,@string)+@splitLen,'')
END
INSERT @result VALUES(@string)
RETURN
END
GO


SELECT * FROM [dbo].[f_splitSTR]('Id1,Id2,Id3,Id4',',')

 

posted @ 2022-01-14 11:32  宋佳莉  阅读(727)  评论(0编辑  收藏  举报