拆分表: > > (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)=',' 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 /
declare @t table(始发站 varchar(10),终点站 varchar(10),车次 varchar(20),中转站 varchar(20)) insert into @t select '站1','站2','车次1' ,null insert into @t select '站1','站2','车次2,车次3' ,'中转站1' insert into @t select '站1','站2','车次4,车次5,车次6','中转站2,中转站3'select a.* from (select parseName(车站,4) as 始发站,parseName(车次,3) as 车次,parseName(车站,3) as 终点站 from (select 始发站+'.'+isnull(replace(中转站+',',',','.'),'')+终点站 as 车站,replace(车次,',','.') as 车次 from @t) t union all select parseName(车站,3),parseName(车次,2),parseName(车站,2) from (select 始发站+'.'+isnull(replace(中转站+',',',','.'),'')+终点站 as 车站,replace(车次,',','.') as 车次 from @t) t union all select parseName(车站,2),parseName(车次,1),parseName(车站,1) from (select 始发站+'.'+isnull(replace(中转站+',',',','.'),'')+终点站 as 车站,replace(车次,',','.') as 车次 from @t) t) a where a.车次 is not null order by a.车次/* 始发站 车次 终点站 ----------- ----------- ----------- 站1 车次1 站2 站1 车次2 中转站1 中转站1 车次3 站2 站1 车次4 中转站2 中转站2 车次5 中转站3 中转站3 车次6 站2 */
declare @t table(始发站 varchar(10),终点站 varchar(10),车次 varchar(20),中转站 varchar(20)) insert into @t select '站1','站2','车次1' ,null insert into @t select '站1','站2','车次2,车次3' ,'中转站1' insert into @t select '站1','站2','车次4,车次5,车次6','中转站2,中转站3' select a.* from (select parseName(车站,4) as 始发站,parseName(车次,3) as 车次,parseName(车站,3) as 终点站 from (select 始发站+'.'+isnull(replace(中转站+',',',','.'),'')+终点站 as 车站,replace(车次,',','.') as 车次 from @t) t union all select parseName(车站,3),parseName(车次,2),parseName(车站,2) from (select 始发站+'.'+isnull(replace(中转站+',',',','.'),'')+终点站 as 车站,replace(车次,',','.') as 车次 from @t) t union all select parseName(车站,2),parseName(车次,1),parseName(车站,1) from (select 始发站+'.'+isnull(replace(中转站+',',',','.'),'')+终点站 as 车站,replace(车次,',','.') as 车次 from @t) t) a where a.车次 is not null order by a.车次 /* 始发站 车次 终点站 ----------- ----------- ----------- 站1 车次1 站2 站1 车次2 中转站1 中转站1 车次3 站2 站1 车次4 中转站2 中转站2 车次5 中转站3 中转站3 车次6 站2 */
> > (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)=','
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
/
insert into @t select '站1','站2','车次1' ,null
insert into @t select '站1','站2','车次2,车次3' ,'中转站1'
insert into @t select '站1','站2','车次4,车次5,车次6','中转站2,中转站3'select
a.*
from
(select parseName(车站,4) as 始发站,parseName(车次,3) as 车次,parseName(车站,3) as 终点站
from (select 始发站+'.'+isnull(replace(中转站+',',',','.'),'')+终点站 as 车站,replace(车次,',','.') as 车次 from @t) t
union all
select parseName(车站,3),parseName(车次,2),parseName(车站,2)
from (select 始发站+'.'+isnull(replace(中转站+',',',','.'),'')+终点站 as 车站,replace(车次,',','.') as 车次 from @t) t
union all
select parseName(车站,2),parseName(车次,1),parseName(车站,1)
from (select 始发站+'.'+isnull(replace(中转站+',',',','.'),'')+终点站 as 车站,replace(车次,',','.') as 车次 from @t) t) a
where
a.车次 is not null
order by
a.车次/*
始发站 车次 终点站
----------- ----------- -----------
站1 车次1 站2
站1 车次2 中转站1
中转站1 车次3 站2
站1 车次4 中转站2
中转站2 车次5 中转站3
中转站3 车次6 站2
*/
declare @t table(始发站 varchar(10),终点站 varchar(10),车次 varchar(20),中转站 varchar(20))
insert into @t select '站1','站2','车次1' ,null
insert into @t select '站1','站2','车次2,车次3' ,'中转站1'
insert into @t select '站1','站2','车次4,车次5,车次6','中转站2,中转站3' select
a.*
from
(select parseName(车站,4) as 始发站,parseName(车次,3) as 车次,parseName(车站,3) as 终点站
from (select 始发站+'.'+isnull(replace(中转站+',',',','.'),'')+终点站 as 车站,replace(车次,',','.') as 车次 from @t) t
union all
select parseName(车站,3),parseName(车次,2),parseName(车站,2)
from (select 始发站+'.'+isnull(replace(中转站+',',',','.'),'')+终点站 as 车站,replace(车次,',','.') as 车次 from @t) t
union all
select parseName(车站,2),parseName(车次,1),parseName(车站,1)
from (select 始发站+'.'+isnull(replace(中转站+',',',','.'),'')+终点站 as 车站,replace(车次,',','.') as 车次 from @t) t) a
where
a.车次 is not null
order by
a.车次 /*
始发站 车次 终点站
----------- ----------- -----------
站1 车次1 站2
站1 车次2 中转站1
中转站1 车次3 站2
站1 车次4 中转站2
中转站2 车次5 中转站3
中转站3 车次6 站2
*/