一行拆分為多行的SQL語句(轉自網絡)


if object_id('[aaa]') is not null drop table [aaa]
go
create table [aaa]([地區] varchar(4),[內容] varchar(45))
insert [aaa]
select '中國','021sp.html|管材|4355;028sp.html|建築材料|3209' union all
select '中國','023sp.html|材|4356;025sp.html|建|9209'
go
  
-->開始查詢
select  a.地區,
        內容=substring(a.內容,b.number,charindex(';',a.內容+';',b.number)-b.number)
from [tb] a
join master..spt_values b on b.type='P'
where substring(';'+a.內容,b.number,1)=';'
  
-->測試結果
---- --------------------------
/*
地區    內容
中國    021sp.html|管材|4355
中國    028sp.html|建築材料|3209
中國    023sp.html|材|4356
中國    025sp.html|建|9209

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM