某列根据逗号分割 转行

 select  ServiceStaff, * from Project_Hdr where Id=12

原查询结果   'XM2053,XM205'

执行下面语句

select
code=substring(a.ServiceStaff,b.number,charindex(',',a.ServiceStaff+',',b.number+1)-b.number),*
from Project_Hdr a,
master..spt_values b
where b.type='p' and b.number<=len(a.ServiceStaff)
and substring(a.ServiceStaff,b.number,1)<>',' and substring(','+a.ServiceStaff,b.number,1)=','
and Id=12

得到结构

posted @ 2013-02-20 09:18  yellowshorts  阅读(285)  评论(0编辑  收藏  举报