1

I need to use REGEXP_REPLACE to do the following :

 If word starts with 'ABCD' then replace first four(4) chars with 'FFFF'
    else
 If word starts with 'XYZ' then replace first three(3) chars with 'GGG'

How do I use REGEXP_REPLACE to do conditional replace ?

1
  • Is the use of REGEXP_REPLACE part of the problem statement, or only part of the solution you want to offer? The best answer (given by Gordon Linoff) does not use it. Commented Jun 4, 2019 at 14:20

2 Answers 2

1

You can use case and string operations:

select (case when word like 'ABCD%'
             then 'FFFF' || substr(word, 5)
             when word like 'XYZ%'
             then 'GGG' || substr(word, 4)
             else word
        end) as new_word
Sign up to request clarification or add additional context in comments.

Comments

1

If it has to be REGEXP_REPLACE you'll have to combine two function calls:

REGEXP_REPLACE( 
  REGEXP_REPLACE(word,'^ABCD','FFFF')
  ,'^XYZ', 'GGG')

But I would prevere Gordon's caseapproach...

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.