返回顶部

收藏

自定义sql split 字符串分割函数

更多
CREATE FUNCTION dbo.split_part(@string varchar(max),@deliminator varchar(10))
RETURNS TABLE AS
RETURN ( 
    WITH p AS (
    SELECT substring(@string, 1, 
        case charindex(@deliminator, @string)   
        WHEN 0  
            THEN len(@string) 
            ELSE charindex(@deliminator, @string) - 1 
        END 
            ) AS parse_val, 
            charindex(@deliminator, @string) AS pos 
    WHERE @string IS NOT NULL   
        AND len(@string) > 0 

    union ALL 
    SELECT substring(@string, pos + 1, 
        case charindex(',', @string, pos + 1)   
            WHEN 0  
            THEN len(@string) - pos 
            ELSE charindex(',', @string, pos + 1) - pos - 1 
            END ) AS parse_val, 
            charindex(',', @string, pos + 1) AS pos 

    FROM p 
    WHERE pos > 0
    )
    SELECT parse_val FROM p
)

标签:sql分割,字符串分割

收藏

0人收藏

支持

0

反对

0