使用Oracle SQL中的一个表达式替换多个子字符串

时间:2017-04-18 08:43:11

标签: oracle replace

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

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

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

在R中它适用于:

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

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

3 个答案:

答案 0 :(得分:3)

您需要REGEXP版本的REPLACE:

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

在你的代码中,你是串联字符串,而不是使用OR运算符;也就是说,您的代码等同于

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

我们可以直接使用解码功能。 选择解码(工作,'职员',' 1','经理'' 2''推销员', ' 3',4)来自emp;

这将取代职员1,经理2,销售员3和其他值4。