0

I am trying to cleanup some text. To achieve that, I came up with following query

Select message,
Case
when REGEXP_Count(message, 'Refreshing|update|create', 1, 'i') > 0 then REGEXP_REPLACE(message, 'Refreshing|Update|create', 'Edited',1, 1, 'i')
when REGEXP_Count(message, 'Open|View', 1, 'i') > 0 then REGEXP_REPLACE(message, 'Open|View', 'Viewed',1, 1, 'i')
else message 
end
From audit_log al 

Is there a better way to handle such replacement so I do not have to repeat REGEXP function twice? I'll need to handle approx 50 different replacements

1
  • Tag your question with the database you are using. Commented May 12, 2021 at 17:56

2 Answers 2

1

You could just replace each value:

REGEXP_REPLACE(REGEXP_REPLACE(message, 'Refreshing|Update|create', 'Edited',1, 1, 'i'
                             ), 'Open|View', 'Viewed', 1, 1, 'i'
              )

This is not 100% equivalent. But assuming that the message has only one of the strings, it should do what you want,.

Sign up to request clarification or add additional context in comments.

Comments

0

With 50 or more of these mappings, it seems to me that you have crossed a threshold and you need a different kind of solution to help you manage and maintain this better.

Why don't you create a simple mapping table with two columns, action and actionclass, then populate it with ('refreshing','edited'), ('update','edited'), etc.

If there are more than one of these keywords in a message then both this solution and your solution behave strangely. This solution returns two instances of the same record with different keywords replaced, while your solution ignores the match of the second keyword entirely.

Select message, Regexp_Replace(message, m.action, m.actiontype,1,1,'i') as newmsg
From audit_log, Actions m
Where instr(message, m.action)> 0

1 Comment

It has to be one query as I am not able to create any mapping tables. On the other hand this is just a one off job, so really not a problem when it comes to maintaining the list.

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.