Oracle 實現拆分列數據的split()方法


-- 創建需要划分的字符串  
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


免責聲明!

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



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