Oracle split函数拆分

Sql代码 复制代码  收藏代码
  1. -- 创建需要划分的字符串   
  2. with T1 as(   
  3.    select 'one,two,three,four,five,six,seven,eight,nine,zero' as source_string   
  4.      from dual),   
  5.       
  6. -- 统计字符串中子串的个数,用 ',' 来划分子串   
  7. T2 as(   
  8.    select regexp_count(source_string, '[^,]+'as source_substring_count   
  9.      from T1),   
  10.       
  11. -- 根据子串的个数创建索引列,用于给T4的regexp_substr()方法索引   
  12. T3 as(   
  13.    select rownum as row_number   
  14.      from dual, T2   
  15.    connect by rownum <= T2.source_substring_count),   
  16.       
  17. -- 根据每个索引值逐个截取字符串   
  18. T4 as(   
  19.    select T3.row_number as substring_index,   
  20.           regexp_substr(T1.source_string, '[^,]+', 1, T3.row_number) as substring  
  21.      from T1, T3)   
  22.       
  23. select substring_index, substring from T4;  
-- 创建需要划分的字符串
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 才新加上的,之前的版本并没有,这里再用另一种方法来统计子串的个数:

Sql代码 复制代码  收藏代码
  1. -- 创建需要划分的字符串   
  2. with T1 as(   
  3.    select 'one,two,three,four,five,six,seven,eight,nine,zero' as source_string   
  4.      from dual),   
  5.       
  6. -- 统计字符串中子串的个数   
  7. -- 字符串中','字符用''代替后,其减少的长度自然就是原串中','字符的个数   
  8. T2 as(   
  9.    select length(T1.source_string) - length(replace(T1.source_string, ',''')) + 1   
  10.           as source_substring_count   
  11.      from T1),   
  12.       
  13. -- 根据子串的个数创建索引列,用于给T4的regexp_substr()方法索引   
  14. T3 as(   
  15.    select rownum as row_number   
  16.      from dual, T2   
  17.    connect by rownum <= T2.source_substring_count),   
  18.       
  19. -- 根据每个索引值逐个截取字符串   
  20. T4 as(   
  21.    select T3.row_number as substring_index,   
  22.           regexp_substr(T1.source_string, '[^,]+', 1, T3.row_number) as substring  
  23.      from T1, T3)   
  24.       
  25. select substring_index, substring from T4;  
-- 创建需要划分的字符串
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;
 

运行结果:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Oracle中没有split函数,但可以使用正则达式或其他方法来实现字符串的拆分。以下是两种常用的方法: 1. 使用正则达式函数REGEXP_SUBSTR 可以使用REGEXP_SUBSTR函数来提取字符串中的子串。例如,要将字符串'apple,orange,banana'按逗号分隔成三个子串,可以使用以下语句: ``` SELECT REGEXP_SUBSTR('apple,orange,banana', '[^,]+', 1, LEVEL) AS result FROM dual CONNECT BY LEVEL <= REGEXP_COUNT('apple,orange,banana', ',') + 1; ``` 其中,[^,]+示非逗号字符的一个或多个,1示从第一个字符开始匹配,LEVEL为循环计数器,CONNECT BY LEVEL示循环次数。执行结果如下: ``` result ------ apple orange banana ``` 2. 使用CONNECT BY LEVEL和SUBSTR函数 可以使用CONNECT BY LEVEL和SUBSTR函数来实现字符串的拆分。例如,要将字符串'apple,orange,banana'按逗号分隔成三个子串,可以使用以下语句: ``` SELECT TRIM(SUBSTR(',' || 'apple,orange,banana', INSTR(',' || 'apple,orange,banana', ',', 1, LEVEL) + 1, INSTR(',' || 'apple,orange,banana', ',', 1, LEVEL + 1) - INSTR(',' || 'apple,orange,banana', ',', 1, LEVEL) - 1)) AS result FROM dual CONNECT BY LEVEL <= REGEXP_COUNT('apple,orange,banana', ',') + 1; ``` 其中,',' || 'apple,orange,banana'示在字符串前添加一个逗号,INSTR函数用于查找逗号的位置,SUBSTR函数用于提取子串,TRIM函数用于去除子串两端的空格。执行结果如下: ``` result ------ apple orange banana ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值