0

Lets say I have

"Hello this is 11 and i want 0032 but there is 013 and 5"

"Hello this is 0011 and i want 0032 but there is 0013 and 0005"

EDITED

2
  • you cannot have it for "any" length. the replacement will cause overflow. Commented Dec 3, 2019 at 12:14
  • So what if I need it for max 4 of them like this sentence? Commented Dec 3, 2019 at 12:27

1 Answer 1

1

I doubt that a single REGEXP_REPLACE is sufficient for your task, but you can do it in 2 steps:

  1. Add some 0-digits before any number so that is at least your desired length.
  2. Delete leading 0-digits until you get your desired length

As code it looks like this:

SELECT 
  REGEXP_REPLACE(
    REGEXP_REPLACE('Hello this is 11 and i want 0032 but there is 013 and 5'
                  ,'(\d+)','000\1') -- Add three 0-digits to any number
                ,'0+(\d{4})','\1') -- Remove all 0-digits prior to the last 4 digits of any number
  FROM dual

Result:

Hello this is 0011 and i want 0032 but there is 0013 and 0005

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.