【问题标题】:Oracle Function for the string stringtokenizer用于字符串 stringtokenizer 的 Oracle 函数
【发布时间】:2019-02-22 19:26:56
【问题描述】:

我是 Oracle PL/SQL 的新手。我需要创建函数来标记以下字符串

test|1$test2|4$test4|5$test9|3

进入下面的行

Key   value 
test    1
test2   4
test4   5
test9   3 

谢谢

【问题讨论】:

  • 到目前为止你有什么尝试?
  • 提示:通过regex_substr(..) 拆分记录,然后插入,并根据需要将返回类型设置为Record,因为它是行和列的混合

标签: sql regex oracle split


【解决方案1】:

您不需要 (PL/SQL) 函数,SQL 可以做到:

SQL> with
  2  -- input string
  3  test (col) as
  4    (select 'test|1$test2|4$test4|5$test9|3' from dual),
  5  -- intermediate CTE which creates rows
  6  inter as
  7    (select regexp_substr(col, '[^$]+', 1, level) val
  8     from test
  9     connect by level <= regexp_count(col, '\$') + 1
 10    )
 11  -- create two columns out of each substring
 12  select substr(val, 1, instr(val, '|') - 1) key,
 13         substr(val, instr(val, '|') + 1) value
 14  from inter;

KEY        VALUE
---------- ----------
test       1
test2      4
test4      5
test9      3

SQL>

但是,如果它必须是一个函数,那么将上面的代码转换成一个应该不会太难。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-27
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多