数据库
首页 > 数据库> > SqlSever 分割函数(split)(搬砖笔记)(有侵权告知即删)

SqlSever 分割函数(split)(搬砖笔记)(有侵权告知即删)

作者:互联网

CREATE FUNCTION dbo.split (
    @String VARCHAR(MAX),
    @Delimiter VARCHAR(MAX)
) RETURNS @temptable TABLE (items VARCHAR(MAX)) AS
BEGIN
    DECLARE @idx INT=1
    DECLARE @slice VARCHAR(MAX) 
    IF LEN(@String) < 1 OR LEN(ISNULL(@String,'')) = 0
        RETURN
    WHILE @idx != 0
    BEGIN
        SET @idx = CHARINDEX(@Delimiter,@String)
        IF @idx != 0
            SET @slice = LEFT(@String,@idx - 1)
        ELSE
            SET @slice = @String
        IF LEN(@slice) > 0
            INSERT INTO @temptable(items) VALUES(@slice)
        SET @String = RIGHT (@String, LEN(@String) - @idx)
        IF LEN(@String) = 0
            BREAK
    END
    RETURN
END

 


 

 

 

标签:slice,VARCHAR,String,idx,LEN,SET,split,SqlSever,侵权
来源: https://www.cnblogs.com/ltcsys/p/13825290.html