【问题标题】:Split Data in SQL Server String在 SQL Server 字符串中拆分数据
【发布时间】:2013-09-04 12:51:06
【问题描述】:

3106 点

967 个帖子 在 SQL Server 字符串中拆分数据

25 分钟前|链接

大家好,

我有两个逗号分隔的字符串,一个有控件名称,另一个有它的值,我已经发送到存储过程以及需要插入数据的表名。

现在,如何在 sql server 2008 中拆分这些字符串并形成插入查询?

另外,我有一个表,我在其中维护了控制值应该存储在哪一列?

所以我需要使用这个来形成插入查询?我该怎么做?

【问题讨论】:

    标签: sql-server-2008


    【解决方案1】:

    此功能将帮助您拆分字符串:

    CREATE function dbo.split(@value varchar(8000),@delim varchar(8000))
    returns table
    as
    return
    (
    select d.value,
           d.orders,
           ivalue = convert(int, case when isnumeric(d.value)=1 and d.value not like '%[^0-9 +-]%' and len(replace(replace(replace(d.value,' ',''),'-',''),'+',''))<=10 then case when convert(bigint,d.value) between -2147483648 and 2147483647 then d.value end end)
    
        from
            (
                select   
                        value= replace(substring(value,
                                                idx,
                                                case when cnt>=0 then cnt end /* case для защиты от нехороших планов, когда сначала идет вычисление substring, а потом ограничивающее where по s_value.number between */
                                             )
                                     ,char(1),'')
                        ,orders=( datalength(left(value,idx-1))-datalength(replace(left(value,idx-1),@delim,''))
                                )/datalength(@delim)
                    from (
                           select number
                                 ,idx
                                 ,cnt = charindex(@delim,value, number + 1) - number - datalength(@delim)
                                 ,value 
                              from 
                                    (
                                       select number
                                             ,idx = number + datalength(@delim)
                                             ,value = (select @delim+char(1)+@value+char(1)+@delim)
                                          from dbo.s_value
                                            where number between 1 and datalength( (select @delim+char(1)+@value+char(1)+@delim) ) - datalength(@delim)
    
                                    ) t            
                              where substring(t.value, number, datalength(@delim)) = @delim         
                         ) t             
           ) d          
    )
    
    
    GO
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-23
      • 1970-01-01
      • 1970-01-01
      • 2018-10-26
      相关资源
      最近更新 更多