码迷,mamicode.com
首页 > 其他好文 > 详细

行转列

时间:2014-10-11 19:55:26      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:style   http   color   io   os   ar   sp   div   on   

9i下
--多行
with temp as
(select ‘6001600301,6001600302,6001600303,6001600304‘ text from dual
union
select ‘7001600301,7001600302,7001600303‘ text from dual
)
select substr(text,instr(text,‘,‘,1,rn)+1,instr(text,‘,‘,1,rn+1)-instr(text,‘,‘,1,rn)-1) text from
(
  select ‘,‘||t1.text||‘,‘ text,t2.rn from
   (select text,length(text)-length(replace(text,‘,‘,‘‘))+1 rn from temp) t1,
   (select rownum rn from all_objects where rownum <= (select max(length(text)-length(replace(text,‘,‘,‘‘))+1) rn from temp)) t2
  where t1.rn >= t2.rn order by text,rn
)

TEXT
6001600301
6001600302
6001600303
6001600304
7001600301
7001600302
7001600303


--单行
with temp as
(select ‘6001600301,6001600302,6001600303,6001600304‘ text from dual)
select substr(text,instr(text,‘,‘,1,rn)+1,instr(text,‘,‘,1,rn+1)-instr(text,‘,‘,1,rn)-1) text from
(
  select ‘,‘||t1.text||‘,‘ text,t2.rn from temp t1,
  (select rownum rn from all_objects where rownum <= (select length(text)-length(replace(text,‘,‘,‘‘))+1 from temp)) t2
)

TEXT
6001600301
6001600302
6001600303
6001600304


10G下
--多行
with temp as
(select ‘6001600301,6001600302,6001600303,6001600304‘ text from dual
union
select ‘7001600301,7001600302,7001600303‘ text from dual
)
select regexp_substr(text,‘[0-9]+‘,1,rn) text from
(
  select t1.text,t2.rn from
   (select text,length(text)-length(replace(text,‘,‘,‘‘))+1 rn from temp) t1,
   (select level rn from dual connect by rownum <= (select max(length(text)-length(replace(text,‘,‘,‘‘))+1) rn from temp)) t2
  where t1.rn >= t2.rn order by text,rn
)

TEXT
6001600301
6001600302
6001600303
6001600304
7001600301
7001600302
7001600303


--单行
with temp as
(select ‘6001600301,6001600302,6001600303,6001600304‘ text from dual)
select regexp_substr(text,‘[0-9]+‘,1,rn) text from temp t1,
(select level rn from dual connect by rownum <= (select length(text)-length(replace(text,‘,‘,‘‘))+1 from temp)) t2

TEXT
6001600301
6001600302
6001600303
6001600304
 




行转列

标签:style   http   color   io   os   ar   sp   div   on   

原文地址:http://www.cnblogs.com/djinmusic/p/4019506.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!