使用SQL Server将连接值分隔为新的单独列

时间:2015-10-25 05:51:17

标签: sql-server

我有一列有多个连接的值作为自由文本输入。现在,我想在新列中隔离每个值,我可以根据最大连接值数来猜测最大列数。

enter image description here

如何实现这一目标? 非常感谢。

2 个答案:

答案 0 :(得分:0)

with CTE  as (
select 'Jhon,Keen,Susan' as Concatenated
union all select 'Jims,Huda' as Concatenated
union all select 'Boul,Rees' as Concatenated
union all select 'Lora' as Concatenated

)
,CTE_xml as (
    select Concatenated,CONVERT(xml,' <root> <s>' + REPLACE(Concatenated,',','</s> <s>') + '</s>   </root> ') as Concatenated_xml
     from CTE
)
,CTE_Unpivot as (
    select CTE_xml.Concatenated,
            T.c.value('.','varchar(max)') as Val,
            'Col'+(T.c.value('for $i in . return count(../*[. << $i]) + 1', 'varchar(max)')) as Name
         from CTE_xml
cross APPLY Concatenated_xml.nodes('/root/s')  T(c)
)

select * from 
    (SELECT CTE_Unpivot.Concatenated, CTE_Unpivot.Val,CTE_Unpivot.Name  from CTE_Unpivot) as Src
    pivot (
        max(src.Val)
        for Name in ([Col1], [Col2], [Col3], [Col4],[Col5],[Col6],[Col7],[Col8])-- in ([Col1], [Col2], [Col3], [Col4])
    )as pvt

这个硬编码到最多8列,如果确定,你可以添加更多。如果您希望它是完全动态的,则必须使Pivot查询变为动态。

答案 1 :(得分:0)

使用动态查询。

declare @columnVar varchar(500);
declare @Query varchar(5000);
/*This first query to get the columns name*/
with CTE  as (
select 'Jhon,Keen,Susan' as Concatenated
union all select 'Jims,Huda' as Concatenated
union all select 'Boul,Rees' as Concatenated
union all select 'Lora' as Concatenated

)
,CTE_xml as (
    select Concatenated,CONVERT(xml,' <root> <s>' + REPLACE(Concatenated,',','</s> <s>') + '</s>   </root> ') as Concatenated_xml
     from CTE
)
,CTE_Unpivot as (
    select CTE_xml.Concatenated,
            T.c.value('.','varchar(max)') as Val,
            'Col'+(T.c.value('for $i in . return count(../*[. << $i]) + 1', 'varchar(max)')) as Name
         from CTE_xml
cross APPLY Concatenated_xml.nodes('/root/s')  T(c)
)
select @columnVar = coalesce(@columnVar + ',', '') +  '['+Name+']' from (select distinct name as name from  CTE_Unpivot) ds

select @Query = ';with CTE  as (
        select ''Jhon,Keen,Susan'' as Concatenated
        union all select ''Jims,Huda'' as Concatenated
        union all select ''Boul,Rees'' as Concatenated
        union all select ''Lora'' as Concatenated

        )
        ,CTE_xml as (
            select Concatenated,CONVERT(xml,'' <root> <s>'' + REPLACE(Concatenated,'','',''</s> <s>'') + ''</s>   </root> '') as Concatenated_xml
             from CTE
        )
        ,CTE_Unpivot as (
            select CTE_xml.Concatenated,
                    T.c.value(''.'',''varchar(max)'') as Val,
                    ''Col''+(T.c.value(''for $i in . return count(../*[. << $i]) + 1'', ''varchar(max)'')) as Name
                 from CTE_xml
        cross APPLY Concatenated_xml.nodes(''/root/s'')  T(c)
        )
        select * from 
        (SELECT CTE_Unpivot.Concatenated, CTE_Unpivot.Val,CTE_Unpivot.Name  from CTE_Unpivot) as Src
        pivot (
            max(src.Val)
            for Name in ('+@columnVar+')-- in ([Col1], [Col2], [Col3], [Col4])
        )as pvt'

exec (@Query)