将列数据拆分为行的SQL查询

21

我有一个SQL表,在其中有两个字段分别为Nodeclaration

Code  Declaration
123   a1-2 nos, a2- 230 nos, a3 - 5nos

我需要显示那段代码的声明:

Code  Declaration 
123   a1 - 2nos 
123   a2 - 230nos 
123   a3 - 5nos

我需要将列数据拆分为行数据,以便使用该代码。

3个回答

23

对于这种数据分离,我建议创建一个分割函数:

create FUNCTION [dbo].[Split](@String varchar(MAX), @Delimiter char(1))       
returns @temptable TABLE (items varchar(MAX))       
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;

然后,要在查询中使用它,您可以使用 outer apply 加入到您现有的表中:

select t1.code, s.items declaration
from yourtable t1
outer apply dbo.split(t1.declaration, ',') s

这将产生以下结果:

| CODE |  DECLARATION |
-----------------------
|  123 |     a1-2 nos |
|  123 |  a2- 230 nos |
|  123 |    a3 - 5nos |

请查看带演示的SQL Fiddle

或者您可以实现类似于以下的CTE版本:

;with cte (code, DeclarationItem, Declaration) as
(
  select Code,
    cast(left(Declaration, charindex(',',Declaration+',')-1) as varchar(50)) DeclarationItem,
         stuff(Declaration, 1, charindex(',',Declaration+','), '') Declaration
  from yourtable
  union all
  select code,
    cast(left(Declaration, charindex(',',Declaration+',')-1) as varchar(50)) DeclarationItem,
    stuff(Declaration, 1, charindex(',',Declaration+','), '') Declaration
  from cte
  where Declaration > ''
) 
select code, DeclarationItem
from cte

很高兴能够帮助。如果这个答案对您有帮助,请务必通过左侧的复选标记接受。这将有助于未来的访问者,而且你也会因为接受而获得声誉! :) - Taryn
由于您的帮助,我得到了以下结果:代码声明 123 a1 - 2个 123 a2 - 230个 123 a3 - 5个 123 a1 - 100个 123 a3 - 6个。是否可以对重复项求和,并将输出显示为:代码声明 123 a1 - 102个 123 a2 - 230个 123 a3 - 11个。 - Affan
@Affan,我已经回答了你的另一个问题,并提供了如何计算值总和的解决方案。 - Taryn
请在这篇文章中为我提供指导...http://stackoverflow.com/questions/15917099/subquery-returned-more-than-1-value-in-insert-statement - Affan

6
Declare @t Table([Code] int, [Declaration] varchar(32));    
Insert Into @t([Code], [Declaration])
Values(123, 'a1-2 nos, a2- 230 nos, a3 - 5nos')

Select 
    x.[Code]
    ,t.Declaration  
    From
    (
        Select 
        *,
        Cast('<X>'+Replace(t.[Declaration],',','</X><X>')+'</X>' As XML) As record

        From @t t
    )x
    Cross Apply
    ( 
        Select fdata.D.value('.','varchar(50)') As Declaration 
        From x.record.nodes('X') As fdata(D)
    ) t

几次以前,我已经在博客中写过相同的内容:使用set基础方法在Sql Server中拆分函数 另外,请访问Erland Sommarskog的博客,他自15年前以来一直在维护相同的答案。

0

尝试一下这个……

declare @col1 varchar(100),@CurentSubString varchar(100)

create table #temp
(
    col1 varchar(50)
)

DECLARE CUR   CURSOR
FOR     SELECT  col1
        FROM    your_table
open    CUR

FETCH   next 
FROM    CUR
INTO    @col1

WHILE @@FETCH_STATUS = 0
BEGIN

    WHILE   CHARINDEX (@col1, ';') <> 0
    BEGIN
    SET @CurentSubString    =   SUBSTRING(@col1,1,CHARINDEX (@col1, ';'))
    SET @col1 = SUBSTRING(@col1,CHARINDEX (@col1, ';')+1,len(@col1))

    insert into #temp
    select @CurentSubString
END


IF CHARINDEX (@col1, ';') = 0 and isnull(@col1,'')!= '' 
BEGIN
    INSERT INTO #temp
    SELECT @col1
END


FETCH   next 
FROM    CUR
INTO    @col1

END


select  * 
From    #temp

CLOSE   CUR
DEALLOCATE CUR

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接