Oracle:分割字符串

oracle數據庫,表數據如下:

ids                           id

3,4,5                        7

13,14,15,16             17

想要使用sql,實現將ids按照逗號分割後查詢到如下記錄:

ids                           id

3                              7

4                              7

5                              7

13                            17

14                            17

15                            17

16                            17 

在Oracle9i以上版本中,可以使用regexp_substr實現。具體sql語句如下:

select id,ids from(

  select regexp_substr(ids, '[^,]+',1,lvl) ids, lvl, id from tbl,

  (select level lvl from dual connect by

  level < =(select max(length(regexp_replace(ids,'[^,]','')))+1 max_tokens from tbl))

) where ids is not null order by lvl

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章