0

I'm using Oracle 11g database which contains data where in I want to replace a dynamically generated text with a dummy text using a oracle query. For e.g my column in table contain data : Hello Mike, Your registered no. is 3525. Kindly check the same .

Now the issue is, Name of customer i.e. 'Mike' can be dynamic that is why I'm not able to use SUBSTR function. And I want to replace 3525 with XXXX E.g Hello Mike, Your registered no. is XXXX. Kindly check the same.

Please help me with the issue. I'm using Oracle 11g

3 Answers 3

2

If the only dynamic part of you string is the name ( and assuming that names do not contain numbers...) you can try:

select regexp_replace('Hello Mike, Your registered no. is 3525. Kindly check the same',
                      '([0-9])',
                      'X'
                     )
from dual

This simply replaces every numeric character with 'X'.

To replace things like, for example, 'AB3525', with a fixed string, say 'XXXX', you can try replacing it with a fixed :

select regexp_replace('Hello Mike, Your registered no. is AB3525. Kindly check the same',
                      '(Hello [^\,]*\, Your registered no. is )([^\.]*)(\. Kindly check the same)',
                      '\1XXXX\3'
                     )            
from dual    
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Aleksej, thats Great! worked for me,. Just an add on question for the same. What if same data contains Alphanumeric value also like for e.g. 'Hello Mike, Your registered no. is ABC12534 and optional no. is 3525'. Here length of ABC12534 varies for different data. Can I've a solution for this too.
2

You could use TRANSLATE which would be much faster than REGULAR EXPRESSION. It would simply any occurrence of a number with X.

For example,

SQL> SELECT TRANSLATE('Hello Mike, Your registered no. is 3525. Kindly check the same',
  2                   '0123456789',
  3                   'XXXXXXXXXX') str
  4  FROM dual;

STR
--------------------------------------------------------------
Hello Mike, Your registered no. is XXXX. Kindly check the same

SQL>

3 Comments

Good to know this "TRANSLATE" function. But it doesn't work now that registered no. can contain alpha chars.
@J.Chomel It will work, just need to tweak accordingly. Can you show me your desired output?
The disered output is asked above by Omkar: What if same data contains Alphanumeric value also like for e.g. 'Hello Mike, Your registered no. is ABC12534 and optional no. is 3525'. Here length of ABC12534 varies for different data.
1

Here is another way: you can split your string in several expression, and access the matches with \{position number}

select regexp_replace('Hello Mike, Your registered no. is 3525. Kindly check the same',
                      '(Hello )(.*)(, Your registered no. is )(.*)(. Kindly check the same)',
                      'Hello \2, Your registered no. is [xxx]. Kindly check the same'
                     )
from dual

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.