sql server split切割字符串

create FUNCTION [dbo].[dnt_split]
(
 @splitstring varchar(max),
 @separator CHAR(1) = ','
)
RETURNS @splitstringstable TABLE
(
 [item] VARCHAR(200)
)
AS
BEGIN
    DECLARE @currentindex INT
    DECLARE @nextindex INT
    DECLARE @returntext VARCHAR(200)

    SELECT @currentindex=1

    WHILE(@currentindex<=datalength(@splitstring))
    BEGIN
        SELECT @nextindex=charindex(@separator,@splitstring,@currentindex)
        IF(@nextindex=0 OR @nextindex IS NULL)
            SELECT @nextindex=datalength(@splitstring)+1
        
        SELECT @returntext=substring(@splitstring,@currentindex,@nextindex-@currentindex)

        INSERT INTO @splitstringstable([item])
        VALUES(@returntext)
        
        SELECT @currentindex=@nextindex+1
    END
    RETURN
END

 

调用方法:

select * from dbo.dnt_split('123,123',',')

 

posted @ 2019-08-13 22:27  沉迷编程的程序员  阅读(247)  评论(0编辑  收藏  举报