SQL 2005在分隔符上拆分逗号分隔列

时间:2009-06-04 15:48:05

标签: sql sql-server-2005

我的谷歌搜索如何在分隔符上拆分字符串已经产生了一些有用的功能,用于在知道字符串时分割字符串(即见下文):

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[Split] (@String varchar(8000), @Delimiter char(1))     
   returns @temptable TABLE (items varchar(8000))       
   as       
   begin       
       declare @idx int       
        declare @slice varchar(8000)       

        select @idx = 1       
            if len(@String)<1 or @String is null  return       

       while @idx!= 0       
       begin       
           set @idx = charindex(@Delimiter,@String)       
           if @idx!=0       
               set @slice = left(@String,@idx - 1)       
           else       
              set @slice = @String       

           if(len(@slice)>0)  
               insert into @temptable(Items) values(@slice)       

           set @String = right(@String,len(@String) - @idx)       
           if len(@String) = 0 break       
       end   
   return       
   end  

这适用于已知的字符串,如:

SELECT TOP 10 * FROM dbo.Split('This,Is,My,List',',')

但是,我想将一个列传递给一个函数,并将它与我自己行中的其他数据联合起来......例如给定数据:

CommaColumn   ValueColumn1   ValueColumn2
-----------   ------------   -------------
ABC,123       1              2
XYZ, 789      2              3

我想写一些像:

SELECT Split(CommaColumn,',') As SplitValue, ValueColumn1, ValueColumn2 FROM MyTable

然后回来

SplitValue    ValueColumn1   ValueColumn2
----------    ------------   ------------
ABC           1              2
123           1              2
XYZ           2              3
789           2              3

这可能,或者之前是否有人这样做过?

5 个答案:

答案 0 :(得分:13)

是的,可以使用CROSS APPLY(SQL 2005 +):

with testdata (CommaColumn, ValueColumn1, ValueColumn2) as (
  select 'ABC,123', 1, 2 union all
  select 'XYZ, 789', 2, 3
  ) 
select 
  b.items as SplitValue
, a.ValueColumn1
, a.ValueColumn2
from testdata a
cross apply dbo.Split(a.CommaColumn,',') b

注意:

  1. 您应该为拆分列的结果集添加索引,以便它返回两列,IndexNumber和Value。

  2. 使用数字表的在线实现通常比此处的程序版本更快。

  3. 例如:

    create function [dbo].[Split] (@list nvarchar(max), @delimiter nchar(1) = N',')
    returns table
    as
    return (
      select 
        Number = row_number() over (order by Number)
      , [Value] = ltrim(rtrim(convert(nvarchar(4000),
            substring(@list, Number
            , charindex(@delimiter, @list+@delimiter, Number)-Number
            )
        )))
      from dbo.Numbers
      where Number <= convert(int, len(@list))
        and substring(@delimiter + @list, Number, 1) = @delimiter
      )
    

    Erland Sommarskog有关于此的最终页面,我认为:http://www.sommarskog.se/arrays-in-sql-2005.html

答案 1 :(得分:10)

以正确的方式修复它 - 将该列设为相关表。以逗号分隔的标量列没有好处。

答案 2 :(得分:1)

+1反CSV注释,但如果你必须这样做,你将使用CROSS APPLY或OUTER APPLY。

答案 3 :(得分:1)

alter procedure [dbo].[usp_split](@strings varchar(max)) as  
begin  
    Declare @index int  
    set @index=1  
    declare @length int  
    set @length=len(@strings)  
    declare @str varchar(max)  
    declare @diff int  
    declare @Tags table(id varchar(30))  
    while(@index<@length)  
    begin  
        if(@index='1')  
        begin  
            set @str=(SELECT substring(@strings, @index, (charindex(',',(substring(@strings, @index,(@length)))))-1))  
            insert into @Tags values(@str)  
                set @index=(charindex(',',(substring(@strings, @index,(@length)))))  
        end  
        else  
        begin  
            set @diff=@length-@index  
            if(@diff !=0)  
            begin  
                set @str=(select substring(@strings, @index, (charindex(',',(substring(@strings,@index,@diff))))-1))  
                if(@str is not null and @str!='')  
                begin  
                    insert into @Tags VALUES(@str)  
                end  
                set @index=@index +(charindex(',',(substring(@strings, @index,@diff))))  
            end  
        end
    end
    set @str=(select right(@strings,(charindex(',',(substring(reverse(@strings),1,(@length)))))-1)) 
    insert into @Tags VALUES(@str)   
    select id from @Tags  
end

用法:

exec usp_split '1212,21213,1,3,133,1313131,1,231313,5'

答案 4 :(得分:0)

您可以尝试以下方式:

SELECT s.Items AS SplitValue, ValueColumn1, ValueColumn2 
FROM MyTable, Split(CommaColumn,',') AS s