部分内容为学习过程中从网上搜集保存备份,以防源网站关闭后无法检索。如有涉及版权请留言,看到后处理

SQLServer Split

ALTER FUNCTION dbo.splitl (
    @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

 

posted @ 2018-12-12 10:28  Y档案Y  阅读(349)  评论(0编辑  收藏  举报