某列根据逗号分割 转行

 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

得到结构

某列根据逗号分割 转行

你可能感兴趣的:(分割)