拆分字符串放入数组

时间:2014-10-28 14:32:23

标签: sql sql-server tsql string-split

我正在处理一个SQL Server面部问题,即分裂字符串。我想实现一个将字符串拆分为数组的函数:

Declare @SQL as varchar(4000)
Set @SQL='3454545,222,555'
Print @SQL

......我必须这样做,我有一个阵列,我有:

total splitCounter=3
Arr(0)='3454545'
Arr(1)='222'
Arr(2)='555'

下面的分割函数并不能满足我的需要,将字符串拆分成数组。

CREATE FUNCTION [dbo].[SplitString]
(
    @String     varchar(max)
,   @Separator  varchar(10)
)
RETURNS TABLE
AS RETURN
(
    WITH
    Split AS (
        SELECT
            LEFT(@String, CHARINDEX(@Separator, @String, 0) - 1) AS StringPart
        ,   RIGHT(@String, LEN(@String) - CHARINDEX(@Separator, @String, 0)) AS RemainingString

        UNION ALL

        SELECT
            CASE
                WHEN CHARINDEX(@Separator, Split.RemainingString, 0) = 0 THEN Split.RemainingString
                ELSE LEFT(Split.RemainingString, CHARINDEX(@Separator, Split.RemainingString, 0) - 1)
            END AS StringPart
        ,   CASE
                WHEN CHARINDEX(@Separator, Split.RemainingString, 0) = 0 THEN ''
                ELSE RIGHT(Split.RemainingString, LEN(Split.RemainingString) - CHARINDEX(@Separator, Split.RemainingString, 0))
            END AS RemainingString
        FROM
            Split
        WHERE
            Split.RemainingString <> ''
    )

    SELECT
        StringPart
    FROM
        Split
)

如果您有任何疑问,请提前致谢。任何类型的建议都将被接受。

3 个答案:

答案 0 :(得分:0)

- 输入

SELECT   * FROM     SplitStringShamim('A,B,C,DDDDDD,EEE,FF,AAAAAAA', ',')


create FUNCTION [dbo].[SplitStringShamim]
(
    @String     varchar(max)
,   @Separator  varchar(10)
)

RETURNS @DataSource TABLE
(
    [ID] TINYINT IDENTITY(1,1)
   ,[Value] NVARCHAR(128)
)   
AS


begin 
            DECLARE @Value NVARCHAR(MAX) = @String

            DECLARE @XML xml = N'<r><![CDATA[' + REPLACE(@Value, @Separator, ']]></r><r><![CDATA[') + ']]></r>'

            INSERT INTO @DataSource ([Value])
                    SELECT RTRIM(LTRIM(T.c.value('.', 'NVARCHAR(128)')))
                    FROM @xml.nodes('//r') T(c)

        return 

end   

答案 1 :(得分:0)

Here

分割功能
CREATE FUNCTION [dbo].[fnSplitString] 
( 
    @string NVARCHAR(MAX), 
    @delimiter CHAR(1) 
) 
RETURNS @output TABLE(splitdata NVARCHAR(MAX) 
) 
BEGIN 
    DECLARE @start INT, @end INT 
    SELECT @start = 1, @end = CHARINDEX(@delimiter, @string) 
    WHILE @start < LEN(@string) + 1 BEGIN 
        IF @end = 0  
            SET @end = LEN(@string) + 1

        INSERT INTO @output (splitdata)  
        VALUES(SUBSTRING(@string, @start, @end - @start)) 
        SET @start = @end + 1 
        SET @end = CHARINDEX(@delimiter, @string, @start)

    END 
    RETURN 
END

从功能中选择:

select *  FROM dbo.fnSplitString('3454545,222,555', ',')

返回

splitdata 
--------
3454545 
222
555

然后使用光标或while循环将每个个体分配给变量(如果您愿意)。 一个表本质上就是一个数组。

答案 2 :(得分:0)

我使用了一些类似的东西,但是我放进了一张桌子,所以我也这样做:

-----------------------------------6----3---4----3---3----4------------------------------------------
declare @cadena varchar(max) = '112331,211,1233,124,533,6341';      
declare @delimitador as char(1) = ',';  
declare @final as int = len(@cadena), @longitudMinima as int = len(reverse(substring(reverse(@cadena),0,charindex(@delimitador,reverse(@cadena)))));
declare @output as table(splits varchar(10));   

/*si no tiene delimitador no tiene sentido, es solo un elemento*/
if charindex(@delimitador,@cadena)>0        
    begin       
        print 'la cadena tiene' + cast(@final as varchar(10)) + ',deberá llegar a:' + cast(@longitudMinima as varchar(10));
        /*se manda hasta la longitud del ultimo elemento*/
        while @final<>@longitudMinima
        begin
            /*mientras se pueda parsear con substring*/
            if charindex(@delimitador,@cadena)>0
            begin
                /*se obtiene elemento de la cadena y la longitud desciende pues la cadena se reduce
                 *cuando se quita este elemento en el substring*/
                declare @element as varchar(100) = substring(@cadena,1,charindex(@delimitador,@cadena)-1);
                set @final = len(@cadena);
                set @cadena = substring(@cadena,charindex(@delimitador,@cadena)+1,@final);
                print 'agregando elemento:' + @element + '--cadena:' + @cadena + '--final:' + cast(@final as varchar(10));
            end
            else
            begin
                set @final = len(@cadena);
                print 'terminando en elemento:' + @cadena + '--cadena:' + @cadena + '--final:' + cast(@final as varchar(10));               
            end
        end     
    end
else 
    begin
        print 'elemento:' + @cadena + '--cadena:' + @cadena;
    end

我要创建功能

create function [dbo].[returnTableFromString](
    @cadena as varchar(max),
    @delimitador as char(1)
)
returns @output table(splits varchar(100))
begin       
    declare @final as int = len(@cadena), @longitudMinima as int = len(reverse(substring(reverse(@cadena),0,charindex(@delimitador,reverse(@cadena)))));            
    /*si no tiene delimitador no tiene sentido, es solo un elemento*/
    if charindex(@delimitador,@cadena)>0        
        begin       
            --print 'la cadena tiene' + cast(@final as varchar(10)) + ',deberá llegar a:' + cast(@longitudMinima as varchar(10));
            /*se manda hasta la longitud del ultimo elemento*/
            while @final<>@longitudMinima
            begin
                /*mientras se pueda parsear con substring*/
                if charindex(@delimitador,@cadena)>0
                begin
                    /*se obtiene elemento de la cadena y la longitud desciende pues la cadena se reduce
                     *cuando se quita este elemento en el substring*/
                    declare @element as varchar(100) = substring(@cadena,1,charindex(@delimitador,@cadena)-1);
                    set @final = len(@cadena);
                    set @cadena = substring(@cadena,charindex(@delimitador,@cadena)+1,@final);
                    insert into @output values (@element)                   
                end
                else
                begin
                    set @final = len(@cadena);
                    insert into @output values (@cadena)                    
                end
            end     
        end
    else 
        begin
            insert into @output values (@cadena);
        end 
    return
end