在Oracle SQL中拆分斜杠

5sxhfpxr  于 2022-11-03  发布在  Oracle
关注(0)|答案(2)|浏览(259)

您好,我需要在Oracle SQL中拆分文本,
输入和输出如图所示。在第一个部分,我有原始数据在一列(在图A列)。在第二个部分,我需要返回(输出)。
我问题是:

select val,
      CASE 
            when substr(val, 1, instr(val, '/') - 1)   is null then   val 
            ELSE substr(val, 1, instr(val, '/') - 1) 
      end as "LEVEL1",
      substr(VAL, 
             instr(val, '/',1)+1, 
             instr(val, '/',1,2)-instr(val, '/',1)-1) "LEVEL2",
      substr(VAL, instr(val, '/') ) "aparat3"
from rmtd1.split_row;

omtl5h9j

omtl5h9j1#

您实际上不需要超过regexp_substr()

select 
  val, 
  regexp_substr (val,'(^|/)([^/]*)',1, 1, null, 2) as level1,
  regexp_substr (val,'(^|/)([^/]*)',1, 2, null, 2) as level2,
  regexp_substr (val,'(^|/)([^/]*)',1, 3, null, 2) as level3,
  regexp_substr (val,'(^|/)([^/]*)',1, 4, null, 2) as level4
from your_table;
  1. (^|/)查找/的开头
  2. ([^/]*)捕获(^|/)每次命中后的所有内容
    1.第三个参数(1)告诉它在输入中从何处开始
    1.第4个参数(1-4)选择返回的匹配项
    1.第5个参数用于调整匹配的确定方式
    1.最后一个参数从上面第2点中的第二个子表达式([^/]*)中选择命中
    demo
gg58donl

gg58donl2#

另一种方法是为了争论。

WITH tbl(val) AS (
SELECT 'aaa/bbbas/cc/dd' FROM dual UNION ALL
SELECT 'xzz/cca' FROM dual UNION ALL
SELECT 'dddddddd/xsssda/asasasasq' FROM dual UNION ALL
SELECT 'ddtrr5fd5sdwe' FROM dual
)
SELECT REGEXP_SUBSTR(val, '(.*?)(/|$)', 1, 1, NULL, 1) AS col_1,
REGEXP_SUBSTR(val, '(.*?)(/|$)', 1, 2, NULL, 1) AS col_2,
REGEXP_SUBSTR(val, '(.*?)(/|$)', 1, 3, NULL, 1) AS col_3,
REGEXP_SUBSTR(val, '(.*?)(/|$)', 1, 4, NULL, 1) AS col_4
FROM tbl;

DBFiddle

相关问题