34,590
社区成员
发帖
与我相关
我的任务
分享
create table T(id int,name varchar(500))
insert T values(11,'AA,BB,CC,DD')
insert T values(22,'XX,YYY,ZZZ,WWWWW')
insert T values(33,'XX,YYY,ZZZ,WWWWW,YYYYY')
go
create function F_test()
returns @T table(ID int,Name nvarchar(500))
as
begin
declare @T2 table(ID int identity,Col int)
insert @T2 select top 501 Col=1 from syscolumns a ,syscolumns b
insert @T
select
a.ID,
substring(a.Name,b.ID,charindex(',',a.Name+',',b.ID)-b.ID)
from
T a
join
@T2 b on substring(','+a.Name,b.ID,1)=','
order by a.ID--加个排序
return
end
go
select
*
from
F_test()
/*
11 AA
11 BB
11 CC
11 DD
22 XX
22 YYY
22 ZZZ
22 WWWWW
33 XX
33 YYY
33 ZZZ
33 WWWWW
33 YYYYY
*/
create table T(id int,name varchar(500))
insert T values(11,'AA,BB,CC,DD')
insert T values(22,'XX,YYY,ZZZ,WWWWW')
go
create function F_test()
returns @T table(ID int,Name nvarchar(500))
as
begin
declare @T2 table(ID int identity,Col int)
insert @T2 select top 501 Col=1 from syscolumns a ,syscolumns b
insert @T
select
a.ID,
substring(a.Name,b.ID,charindex(',',a.Name+',',b.ID)-b.ID)
from
T a
join
@T2 b on substring(','+a.Name,b.ID,1)=','
return
end
go
select
*
from
F_test()
/*
11 AA
22 XX
11 BB
22 YYY
11 CC
22 ZZZ
11 DD
22 WWWWW
*/
create table tb(id int,name varchar(500))
insert tb values(11,'AA,BB,CC,DD')
insert tb values(22,'XX,YY,ZZ,WW')
select id,name
from
(
select a.id,name=substring(a.name,b.id,charindex(',',a.name+',',b.id)-b.id)
from tb a,(select id=1 union all select 2 union all select 3 union all select 4 union all select 5
union all select 6 union all select 7 union all select 8 union all select 9
union all select 10 union all select 11 union all select 12) b
where b.id<=len(a.name) and substring(','+a.name,b.id,1)=','
) x
drop table tb
create table tb(id int,name varchar(500))
insert tb values(11,'AA,BB,CC,DD')
insert tb values(22,'XX,YY,ZZ,WW')
select top 500 id=identity(int,1,1) into # from syscolumns a,syscolumns b
select *
from
(
select a.id,name=substring(a.name,b.id,charindex(',',a.name+',',b.id)-b.id)
from tb a,# b
where b.id<=len(a.name) and substring(','+a.name,b.id,1)=','
) x
drop table tb,#
/*
id name
----------- ----------
11 AA
11 BB
11 CC
11 DD
22 XX
22 YY
22 ZZ
22 WW
(所影响的行数为 8 行)
*/
create table tb(id int,name varchar(500))
insert tb values(11,'AA,BB,CC,DD')
select top 500 id=identity(int,1,1) into # from syscolumns a,syscolumns b
select *
from
(
select a.id,name=substring(a.name,b.id,charindex(',',a.name+',',b.id)-b.id)
from tb a,# b
where b.id<=len(a.name) and substring(','+a.name,b.id,1)=','
) x
drop table tb,#
/*
id name
----------- ----------
11 AA
11 BB
11 CC
11 DD
(所影响的行数为 4 行)
*/
declare @t table (id int,name varchar(40))
insert into @t select 1,'AA,BB,CC,DD'
select id,parseName(replace(name,',','.'),4) from @t union all
select id,parseName(replace(name,',','.'),3) from @t union all
select id,parseName(replace(name,',','.'),2) from @t union all
select id,parseName(replace(name,',','.'),1) from @t