2

I would like to delete multiple substrings from one column. I tried the replace function with the following code:

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

My expected result is ttt, but I get tstst.

In R it works with:

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

How can I replace many different substrings with nothing ('') in a column in clob format in Oracle SQL?

3 Answers 3

3

You need the REGEXP version of REPLACE:

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

In your code, you are concatenating strings, instead of using an OR operator; that is, your code is equivalent to

select replace('testetstestetststst', 'testets', '')
    from dual;
Sign up to request clarification or add additional context in comments.

Comments

0

Rather than using regular expressions, you can nest multiple REPLACE functions:

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

Comments

0

We can directly use decode function. select decode(job,'clerk','1','manager','2','salesman','3',4) from emp;

This will replace clerk with 1,manager with 2,salesman with 3 and other values with 4.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.