Oracle 實現拆分列資料的split()方法

來源:互聯網
上載者:User

標籤:one   connect   實現   --   截取字串   count()   截取   rownum   nec   

-- 建立需要劃分的字串  with T1 as(     select ‘one,two,three,four,five,six,seven,eight,nine,zero‘ as source_string       from dual),       -- 統計字串中子串的個數,用 ‘,‘ 來劃分子串  T2 as(     select regexp_count(source_string, ‘[^,]+‘) as source_substring_count       from T1),       -- 根據子串的個數建立索引列,用於給T4的regexp_substr()方法索引  T3 as(     select rownum as row_number       from dual, T2     connect by rownum <= T2.source_substring_count),       -- 根據每個索引值逐個截取字串  T4 as(     select T3.row_number as substring_index,            regexp_substr(T1.source_string, ‘[^,]+‘, 1, T3.row_number) as substring       from T1, T3)       select substring_index, substring from T4;

鑒於 regexp_count() 方法是 Oracle 11g 才新加上的,之前的版本並沒有,這裡再用另一種方法來統計子串的個數:

-- 建立需要劃分的字串  with T1 as(     select ‘one,two,three,four,five,six,seven,eight,nine,zero‘ as source_string       from dual),       -- 統計字串中子串的個數  -- 字串中‘,‘字元用‘‘代替後,其減少的長度自然就是原串中‘,‘字元的個數  T2 as(     select length(T1.source_string) - length(replace(T1.source_string, ‘,‘, ‘‘)) + 1            as source_substring_count       from T1),       -- 根據子串的個數建立索引列,用於給T4的regexp_substr()方法索引  T3 as(     select rownum as row_number       from dual, T2     connect by rownum <= T2.source_substring_count),       -- 根據每個索引值逐個截取字串  T4 as(     select T3.row_number as substring_index,            regexp_substr(T1.source_string, ‘[^,]+‘, 1, T3.row_number) as substring       from T1, T3)       select substring_index, substring from T4;  

看見的一個博主寫的,正好自己能用,先記下,同時感謝這位博主

原連結:http://flforever1213.iteye.com/blog/1026096

Oracle 實現拆分列資料的split()方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.