1

I have one oracle table with one of the columns having below values,

,21A,22,21,28,28

I want to do a regex replace for removal of ,21 (The exact match) but it removes the value from ,21A

Below is my query,

update STUDENT set SCORES = REGEXP_REPLACE(SCORE, ',21' , '', 1, 1) where STUDENT_ID = 1;

How can this be achieved? Any ideas?

5
  • @AlexanderDerck, This isn't working. Commented Feb 18, 2016 at 22:43
  • My bad apparantly oracle regex has no lookarounds. Can't you just check for ,21, and replace with , instead of empty string then? Commented Feb 18, 2016 at 22:45
  • Can you fix the database design instead and save yourself other hassles down the road? This column violates 1st normal form. If you are stuck with someone else's bad design I feel your pain. Commented Feb 18, 2016 at 23:00
  • @Gary_W, This is already implemented and running. Design change will trigger lot of other changes at this time. Commented Feb 18, 2016 at 23:06
  • Maybe REGEXP_REPLACE(SCORE, ',21(,|$)' , '\1', 1, 1) is enough? Or even REGEXP_REPLACE(SCORE, ',21(\W|$)' , '\1', 1, 1)? Commented Feb 18, 2016 at 23:22

1 Answer 1

2

In your case, you can use a ,21(\W|$) regex. It matches ,21 that is followed with a non-word character or the end of string.

REGEXP_REPLACE(SCORE, ',21(\W|$)' , '\1', 1, 1)

Here is how you can perform a whole word search and replace in Oracle SQL:

regexp_replace( 
    haystack
    , '(\W|^)(' || what_to_replace || ')(\W|$)'
    , '\1' || with_what || '\3'
    , 1
    , 0
    , 'imn'
)
Sign up to request clarification or add additional context in comments.

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.