【问题标题】:Take substring from a string dynamically in Oracle v11/v12在 Oracle v11/v12 中动态地从字符串中获取子字符串
【发布时间】:2021-06-09 10:12:57
【问题描述】:

我有这些字符串,在 Oracle 中我应该动态获取子字符串。

Input:
Record: 11, Entità : CustomerOrder, attributo: DESCRIPTION non conforme al formato atteso
Output:
Entità : CustomerOrder, attributo: DESCRIPTION non conforme al formato atteso

Input:
Record: 3, Entità : CustomerOrder, attributo: DESCRIPTION non conforme al formato atteso
Output:
Entità : CustomerOrder, attributo: DESCRIPTION non conforme al formato atteso

您能帮我解答一下吗?

非常感谢

【问题讨论】:

  • 你应该说识别子串的规则是什么,没有它我们只能猜测并提供3+不同的答案

标签: sql oracle oracle11g oracle12c substr


【解决方案1】:

只需使用INSTR 查找第一个逗号的索引,然后取后面 2 个字符开始的子字符串(跳过逗号和空格):

SELECT SUBSTR( input, INSTR( input, ',' ) + 2 ) AS output
FROM   table_name;

其中,对于样本数据:

CREATE TABLE table_name ( input ) AS
SELECT 'Record: 11, Entità : CustomerOrder, attributo: DESCRIPTION non conforme al formato atteso' FROM DUAL UNION ALL
SELECT 'Record: 3, Entità : CustomerOrder, attributo: DESCRIPTION non conforme al formato atteso' FROM DUAL;

输出:

|输出 | | :------------------------------------------------- ---------------------------------------- | |实体:客户订单,属性:说明不符合格式要求 | |实体:客户订单,属性:说明不符合格式要求 |

db小提琴here

【讨论】:

    【解决方案2】:

    考虑到您需要提取以模式Entità : 开始直到行尾的子字符串,使用REGEXP_REPLACE()函数如

    SELECT 'Entità : '||REGEXP_REPLACE(col,'(.*Entità : )(.*)','\2') AS output
      FROM t;
    
    OUTPUT
    ----------------------------------------------------------------------------- 
    Entità : CustomerOrder, attributo: DESCRIPTION non conforme al formato atteso
    Entità : CustomerOrder, attributo: DESCRIPTION non conforme al formato atteso
    

    Demo

    【讨论】:

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