22,209
社区成员
发帖
与我相关
我的任务
分享
with t(dwbh,jingyfw) as (
select 'DWI00000001',N'中成药,化学制剂,生化药品,生物制品' union
select 'DWI00000002',N'中药片,中药饮片'
),tt(dwbh,xjingyfw) as(
select t.dwbh,convert(xml,'<r><n>'+replace(t.jingyfw,',','</n><n>')+'</n></r>') from t
)
select tt.dwbh,ttt.yp from tt
cross apply(select b.value('.','nvarchar(max)') as yp from tt.xjingyfw.nodes('r/n') as s(b)) ttt
结果:
dwbh yp
DWI00000001 中成药
DWI00000001 化学制剂
DWI00000001 生化药品
DWI00000001 生物制品
DWI00000002 中药片
DWI00000002 中药饮片
declare @tb table(col varchar(50));
insert into @tb
select '10-2-6' union all
select '10-15-6' union all
select '14-8-2' union all
select '1-0-0';
with cte as(
select tb.col,
SUBSTRING(tb.col,s.number+1,(case when CHARINDEX('-',tb.col,s.number+1)>0 then CHARINDEX('-',tb.col,s.number+1)-1 else LEN(tb.col) end)-s.number) as 'a',
_index=len(substring(tb.col,0,s.number+1))-len(replace(substring(tb.col,0,s.number+1),'-',''))
from @tb tb,master..spt_values s where s.type='p' and s.number between 0 and LEN(tb.col) and ( SUBSTRING(tb.col,s.number,1)='-' or s.number=0)
)
select * from cte
仅供参考create table #t1(dwbh nvarchar(50),jingwfw nvarchar(500))
select dwbh,charindex(',',jingwfw,1)pos into #t2 from #t
select dwbh,0 as pos into #t3 from #t
while exists (select * from #t2 where pos<>0)
begin
insert into #t1
select a.dwbh,SUBSTRING(a.jingwfw,c.pos+1,b.pos-c.pos-1) from #t a
left join #t2 b on a.dwbh=b.dwbh
left join #t3 c on a.dwbh=c.dwbh
where b.pos<>0
delete from #t3
insert into #t3 select * from #t2
update a set pos=CHARINDEX(',',b.jingwfw+',',a.pos+1)
from #t2 a inner join #t b on a.dwbh=b.dwbh where a.pos<>0
end
select * from #t1
drop table #t1
drop table #t2
drop table #t3
你試試這個