【问题标题】:SQL String Delimited by Comma into a Table以逗号分隔的 SQL 字符串到表中
【发布时间】:2017-02-01 02:31:01
【问题描述】:

我有一个用逗号分隔的数字字符串。样品:123, 213, 312, 231 我需要处理 SQL 游标中的每个数字。所以我的第一步是将每个数字放在一个表格中,如下所示:

DECLARE @t AS TABLE(string nvarchar(100));

但问题是我不知道如何拆分字符串、删除逗号并将每个数字插入我同时创建的表中。我可以尝试猴子尝试努力编码,但我知道它不会既漂亮又快速。请帮帮我!

注意:我使用的是 SQL Server 2012,但如果该功能也支持 SQL Server 2008 R2,那就太好了。

【问题讨论】:

    标签: sql sql-server string sql-server-2012 sql-server-2008-r2


    【解决方案1】:

    rextester:http://rextester.com/ZCU48506

    功能:Jeff Moden 的 Split N4k

    create function dbo.DelimitedSplitN4K (
        @pString nvarchar(4000)
      , @pDelimiter nchar(1)
      )
    returns table with schemabinding as
    return
      with e1(n) as (
        select 1 union all select 1 union all select 1 union all 
        select 1 union all select 1 union all select 1 union all 
        select 1 union all select 1 union all select 1 union all select 1
      )
      , e2(n) as (select 1 from e1 a, e1 b)
      , e4(n) as (select 1 from e2 a, e2 b)
      , cteTally(n) as (select top (isnull(datalength(@pString)/2,0))
          row_number() over (order by (select null)) from e4)
      , cteStart(n1) as (select 1 union all 
          select t.n+1 from cteTally t where substring(@pString,t.n,1) = @pDelimiter)
      , cteLen(n1,l1) as(select s.n1
      ,   isnull(nullif(charindex(@pDelimiter,@pString,s.n1),0)-s.n1,4000)
        from cteStart s
      )
     select ItemNumber = row_number() over(order by l.n1)
          , Item       = substring(@pString, l.n1, l.l1)
       from cteLen l;
    go
    

    查询:

    declare @sample nvarchar (64) = '123,213,312,231'
    
    select * from dbo.DelimitedSplitN4K(@sample,',')
    

    结果

    +------------+------+
    | ItemNumber | Item |
    +------------+------+
    |          1 |  123 |
    |          2 |  213 |
    |          3 |  312 |
    |          4 |  231 |
    +------------+------+
    

    拆分字符串参考:

    【讨论】:

    • 哇,这看起来太棒了。非常感谢您链接此自定义工具!将标记为答案
    【解决方案2】:

    您可以使用 XML 函数,因为它更快。

    首先,创建函数:

    CREATE FUNCTION stringDilimitedToTableXML
    (   
        @str VARCHAR(4000),
        @delimiter nchar(1)
    )
    RETURNS @Result TABLE(Value BIGINT)
    AS
    BEGIN
    
        Declare @x XML 
        select @x = cast('<A>' + replace(@str,@delimiter,'</A><A>') + '</A>' as xml)
    
        INSERT INTO @Result
            SELECT t.value('.', 'int') as inVal
            FROM @x.nodes('/A') as x(t)
    
        RETURN
    
    END
    GO
    

    在查询时调用函数:

    DECLARE @str VARCHAR(4000) = '2879,2880,2881,2892,2893,2894'
    
    SELECT * FROM dbo.stringDilimitedToTableXML( @str, ',')  
    

    结果:

    Value
    --------------------
    2879
    2880
    2881
    2892
    2893
    2894
    
    (6 row(s) affected)
    

    链接:https://blogs.msdn.microsoft.com/amitjet/2009/12/11/convert-comma-separated-string-to-table-4-different-approaches/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多