DECLARE  @T TABLE
(
Air NVARCHAR(30),
NameT NVARCHAR(100)
)INSERT INTO @T
select 'UA,NW,CX,TD','IAM1' 
UNION ALL
select 'UA,NW,LH,TD','IAM2'
select * from @T根据Air拆分,想得到如下数据:
UA,IAM1
NW,IAM1
CX,IAM1
TD,IAM1
UA,IAM2
NW,IAM2
LH,IAM2
TD,IAM2

解决方案 »

  1.   

    拆分函数一大堆,参考ROY大哥博客,如果是4个分段,考虑用PARSENAME函数
      

  2.   

    select parsename(Air,1),NameT from @t union all
    select parsename(Air,2),NameT from @t union all
    select parsename(Air,3),NameT from @t union all
    select parsename(Air,4),NameT from @t 
      

  3.   

    --参考
    拆分表:--> --> (Roy)生成測試數據
     
    if not object_id('Tab') is null
        drop table Tab
    Go
    Create table Tab([Col1] int,[COl2] nvarchar(5))
    Insert Tab
    select 1,N'a,b,c' union all
    select 2,N'd,e' union all
    select 3,N'f'
    Go--SQL2000用辅助表:
    if object_id('Tempdb..#Num') is not null
        drop table #Num
    go
    select top 100 ID=Identity(int,1,1) into #Num from syscolumns a,syscolumns b
    Select 
        a.Col1,COl2=substring(a.Col2,b.ID,charindex(',',a.Col2+',',b.ID)-b.ID) 
    from 
        Tab a,#Num b
    where
        charindex(',',','+a.Col2,b.ID)=b.ID --也可用 substring(','+a.COl2,b.ID,1)=','
    --2000不使用辅助表
    Select
        a.Col1,COl2=substring(a.Col2,b.number,charindex(',',a.Col2+',',b.number)-b.number) 
    from 
        Tab a join master..spt_values  b 
        ON B.type='p' AND B.number BETWEEN 1 AND LEN(A.col2)
    where
         substring(','+a.COl2,b.number,1)=','
    SQL2005用Xml:select 
        a.COl1,b.Col2
    from 
        (select Col1,COl2=convert(xml,'<root><v>'+replace(COl2,',','</v><v>')+'</v></root>') from Tab)a
    outer apply
        (select Col2=C.v.value('.','nvarchar(100)') from a.COl2.nodes('/root/v')C(v))b
    SQL05用CTE:;with roy as 
    (select Col1,COl2=cast(left(Col2,charindex(',',Col2+',')-1) as nvarchar(100)),Split=cast(stuff(COl2+',',1,charindex(',',Col2+','),'') as nvarchar(100)) from Tab
    union all
    select Col1,COl2=cast(left(Split,charindex(',',Split)-1) as nvarchar(100)),Split= cast(stuff(Split,1,charindex(',',Split),'') as nvarchar(100)) from Roy where split>''
    )
    select COl1,COl2 from roy order by COl1 option (MAXRECURSION 0)生成结果:
    /*
    Col1        COl2
    ----------- -----
    1           a
    1           b
    1           c
    2           d
    2           e
    3           f
    */
      

  4.   

    DECLARE  @T TABLE
    (
        Air NVARCHAR(30),
        NameT NVARCHAR(100)
    )INSERT INTO @T
    select 'UA,NW,CX,TD','IAM1' 
    UNION ALL
    select 'UA,NW,LH,TD','IAM2'
    select a.NameT, b.Air  
    from   
    (   
       select NameT, Air = convert(xml,'<v>' + replace(Air, ',', '</v><v>') + '</v>') from @t   
    ) a   
    outer apply   
    (   
       select Air = n.v.value('.', 'varchar(10)') from a.Air.nodes('/v') n(v)   
    ) b   
    /*
    NameT Air
    IAM1 UA
    IAM1 NW
    IAM1 CX
    IAM1 TD
    IAM2 UA
    IAM2 NW
    IAM2 LH
    IAM2 TD
    */
      

  5.   


    DECLARE  @T TABLE
    (
        Air NVARCHAR(30),
        NameT NVARCHAR(100)
    )INSERT INTO @T
    select 'UA,NW,CX,TD','IAM1' 
    UNION ALL
    select 'UA,NW,LH,TD','IAM2'select air = substring(air,b.number,charindex(',',air+',',charindex(',',a.air+',',b.number))-b.number)
           ,NameT
    from @T a
         inner join master..spt_values b
    on charindex(',',','+a.air,b.number) = b.number 
    and type = 'P'--结果
    /*
    air                            NameT
    ------------------------------ ----------------------------------------------------------------------------------------------------
    UA                             IAM1
    NW                             IAM1
    CX                             IAM1
    TD                             IAM1
    UA                             IAM2
    NW                             IAM2
    LH                             IAM2
    TD                             IAM2(8 行受影响)
    */