日期:2014-05-19  浏览次数:20473 次

SQL分割的问题
数据表class中有一个字段为Type,如下:

id                   type
1                   001,002,003
2                   003,002
3                   009,004,008,005

当ID为1时,我想把type字段的值如下显示:
tt
001
002
003

------解决方案--------------------
declare @a table(id int,type varchar(100))
insert @a select 1, '001,002,003 '
union all select 2, '003,002 '
union all select 3, '009,004,008,005 '


select top 100 id=identity(int,1,1) into # from syscolumns
select a.id,
substring(type,b.id,charindex( ', ',type+ ', ',b.id+1)-b.id) value
from @a a ,
# b
where substring( ', '+a.type,b.id,1)= ', '

drop table #

------解决方案--------------------
--result
/*
id value
----------- ------------
1 001
1 002
1 003
2 003
2 002
3 009
3 004
3 008
3 005

(所影响的行数为 9 行)
*/