Fn_StringSplitToTable

ALTER FUNCTION [dbo].[Fn_StringSplitToTable] 
( 
    @StringX varchar(MAX), 
    @Split nvarchar(10)
) 
RETURNS 
@IdsTable TABLE 
( 
    [Id] nvarchar(200) 
) 
AS 
BEGIN 
    DECLARE @Index int 
    DECLARE @LenIndex int 
    SET @LenIndex=LEN(@Split) 
    SET @Index=CHARINDEX(@Split,@StringX,1) 
    WHILE (@Index>=1) 
    BEGIN 
        IF(LEFT(@StringX,@Index-1) <> '') 
            INSERT INTO @IdsTable SELECT LTRIM(RTRIM(LEFT(@StringX,@Index-1))) 
        SET @StringX=RIGHT(@StringX,LEN(@StringX)-@Index-@LenIndex+1) 
        SET @Index=CHARINDEX(@Split,@StringX,1) 
    END 
    IF(@StringX<>'') INSERT INTO @IdsTable SELECT LTRIM(RTRIM(@StringX)) 
    
    RETURN 
END

  

posted @ 2024-03-20 14:25  萌橙  阅读(8)  评论(0编辑  收藏  举报