【问题标题】:Replace multiple substrings with one expression in Oracle SQL在 Oracle SQL 中用一个表达式替换多个子字符串
【发布时间】:2018-02-19 14:00:07
【问题描述】:

我想从一列中删除多个子字符串。我用以下代码尝试了替换功能:

select replace('testetstestetststst', 'test'||'et'||'s', '')
    from dual;

我的预期结果是ttt,但我得到了tstst

在 R 中,它适用于:

gsub("test|et|s", "", "testetstestetststst")

如何在 Oracle SQL 中以 clob 格式的列中替换许多不同的子字符串 ('')

【问题讨论】:

    标签: oracle replace


    【解决方案1】:

    您需要 REGEXP 版本的 REPLACE:

    select regexp_replace('testetstestetststst', 'test|et|s', '')
        from dual;
    

    在您的代码中,您正在连接字符串,而不是使用 OR 运算符;也就是说,你的代码相当于

    select replace('testetstestetststst', 'testets', '')
        from dual;
    

    【讨论】:

      【解决方案2】:

      您可以嵌套多个REPLACE函数,而不是使用正则表达式:

      SELECT REPLACE(
               REPLACE(
                 REPLACE(
                   'testetstestetststst',
                   'test'
                 ),
                 'et'
               ),
               's'
             )
      FROM   DUAL;
      

      【讨论】:

        【解决方案3】:

        我们可以直接使用decode函数。 select decode(job,'clerk','1','manager','2','salesman','3',4) from emp;

        这会将 clerk 替换为 1,manager 替换为 2,salesman 替换为 3,其他值替换为 4。

        【讨论】:

          猜你喜欢
          • 2015-05-11
          • 1970-01-01
          • 1970-01-01
          • 2011-04-12
          • 2019-04-17
          • 2017-12-06
          • 1970-01-01
          • 2014-01-11
          • 1970-01-01
          相关资源
          最近更新 更多