日期:2014-05-16  浏览次数:20438 次

求一个关于切割的sql语法
有表
A     B   (这里是列名)
1     1,2,3
2     2,3,6


查询出来得到
A     B
1     1
1     2
1     3
2     2
2     2
2     6 
------解决方案--------------------
试试这个:

--drop table t

create table t(A int,B varchar(30))


insert into t
select 1     ,'1,2,3' union all
select 2     ,'2,3,6'
go

select A,
       SUBSTRING(t.B, number ,CHARINDEX(',',t.B+',',number)-number) B
from t,master..spt_values s
where s.number >=1
and s.type = 'P'
and SUBSTRING(','+t.B,s.number,1) = ','
/*
A B
1 1
1 2
1 3
2 2
2 3
2 6
*/