日期:2014-05-16 浏览次数:20623 次
select 1 as tname,'01:0102;02:0102;03:0102;04:0102,0101;05:0102'as tstr into tb1
insert into tb1 select 2,'01:0101,0102'
insert into tb1 select 3,'01:0102;02:0102;03:0102;04:0102;05:0102'
insert into tb1 select 4,'01:0102,0103'
insert into tb1 select 5,'0104'
insert into tb1 select 6,'01:0102;02:0102;03:0102;04:0102;05:0102'
insert into tb1 select 7,'01:0102;02:0102;03:0102;04:0102,0101;05:0102'
select --*,
distinct
case when vv like '%:%' then SUBSTRING(vv,charindex(':',vv)+1,len(vv))
else vv
end '编组'
from
(
select tname,
tstr,
v,
SUBSTRING(t.v, number ,CHARINDEX(',',t.v+',',number)-number) vv
from
(
select tname,
tstr,
SUBSTRING(t.tstr, number ,CHARINDEX(';',t.tstr+';',number)-number) v
from tbl t,master..spt_values s
where s.number >=1
and s.type = 'P'
and SUBSTRING(';'+t.tstr,s.number,1) = ';'
)t,master..spt_values s
where s.number >=1
and s.type = 'P'
and SUBSTRING(','+t.v,s.number,1) = ','
)t
/*
编组
0101
0102
0103
0104
*/
SELECT * INTO #tb
FROM
(
Select
a.tname,tstr=substring(substring(a.tstr,b.number,charindex(';',a.tstr+';',b.number)-b.number),4,LEN(substring(a.tstr,b.number,charindex(';',a.tstr+';',b.number)-b.number)))
from
Tb1 a join master..spt_values b
ON B.type='p' AND B.number BETWEEN 1 AND LEN(A.tstr)
where
substring(';'+a.tstr,b.number,1)=';')t
SELECT * FROM
(
SELECT PARSENAME(REPLACE(tstr,',','.'),2) AS col FROM #tb
UNION
SELECT PARSENAME(REPLACE(tstr,',','.'),1) FROM #tb)t WHERE col IS NOT NULL
--drop table tbl
create table tbl(tname int,tstr varchar(100))
insert into tbl
select 1 ,'01:0102;02:0102;03:0102;04:0102,0101;05:0102'
insert into tbl
select 2,'01:0101,0102'
insert into tbl
select 3,'01:0102;02:0102;03:0102;04:0102;05:0102'
insert into&nb