22,210
社区成员
发帖
与我相关
我的任务
分享
SELECT T1.SalesCode,T2.V FROM
(SELECT SalesCode,CAST('<V>'+REPLACE(TeamBelow,',','</V><V>')+'</V>' AS XML)VS FROM TB)T1
CROSS APPLY(SELECT N.V.value('.','VARCHAR(100)')V FROM T1.VS.nodes('/V') N(V))T2
create table ah
(SalesCode varchar(10),TeamBelow varchar(10))
insert into ah
select 'CD0001','a,b,c' union all
select 'CD0002','h,i,j'
select a.SalesCode,
substring(a.TeamBelow,b.number,charindex(',',a.TeamBelow+',',b.number)-b.number) 'TeamBelow'
from ah a,master.dbo.spt_values b
where b.type='P' and b.number between 1 and len(a.TeamBelow)
and substring(','+a.TeamBelow,b.number,1)=','
/*
SalesCode TeamBelow
---------- ----------
CD0001 a
CD0001 b
CD0001 c
CD0002 h
CD0002 i
CD0002 j
(6 row(s) affected)
*/