【问题标题】:oracle, extract multiple substring from a stringoracle,从字符串中提取多个子字符串
【发布时间】:2019-02-20 19:44:41
【问题描述】:

试图从下面的字符串中提取两个数值(8 和 10)作为两个新列。

试图从this问题的答案中受益,但无法解决这个问题。

任何帮助表示赞赏!

'{"someInconsiderableText1": {"answer": "8", "question": "someInconsiderableText2"}, "someInconsiderableText3": {"answer": "10", "question": "someInconsiderableText4"}}'

【问题讨论】:

  • 您的问题对于您想要做什么有点含糊。您只想要数字 8 和 10 还是正在寻找答案对象的任何 JSON 值?以及什么版本的 Oracle(较新的版本有 JSON 解析库)?
  • @Sam M,我上面提供的字符串是 Oracle 数据仓库(Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production)中的 CLOB 字段的值。该字段包括两个独立问题的答案。我需要的是选择/提取这些问题的答案作为两个不同的领域。希望现在更清楚了。简而言之,我需要两个新字段,第一个字段的值为“8”,第二个字段的值为“10”。
  • Oracle 12c 中引入了本机 JSON 支持。我已经阅读过这个用于 Oracle 的第三方 JSON 解析库,但从未使用过它 --> stackoverflow.com/questions/30393214/…。尝试使用正则表达式操作 JSON 字符串很诱人,但很快就会变得复杂并且容易出错。您最好尝试使用 JSON 库。

标签: sql regex string oracle substring


【解决方案1】:

这会有帮助吗?

SQL> with test (col) as
  2    (select '{"someInconsiderableText1": {"answer": "8", "question": "someInconsiderableText2"},
"someInconsiderableText3": {"answer": "10", "question": "someInconsiderableText4"}}'
  3     from dual
  4    )
  5  select replace(regexp_substr(col, '"\d+"', 1, 1), '"') first,
  6         replace(regexp_substr(col, '"\d+"', 1, 2), '"') second
  7  from test;

FIRST      SECOND
---------- ----------
8          10

SQL>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-23
    • 2021-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多