Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

how to replace REPLACE

1932140Nov 28 2014 — edited Nov 30 2014

Hi

using 11.2.0.4 EE.

How can I replace the following:

"replace(replace(replace(config_item,'COLLATERAL','COLLAT'),'AGREEMENT','AGREE'),'VALUE','VAL')"

with a single regular expression or something simpler ?

The reason is I need something simpler since more strings will be required to replace than simply the three currently listed.

Thanks

Peter

This post has been answered by Deepak Mahto on Nov 28 2014
Jump to Answer

Comments

Frank Kulash

Hi, Peter,

Sorry, there's nothing quite like that built into Oracle.

When I've needed to do that, I created a table, with separate columns for the old and new strings (e.g. old_str='COLLATERAL' and new_str='COLLAT'.)  Then, in a user-defined function, I looped through all of the pairs and did the REPLACEs, 1 at a time.

Instead of a user-defined function, you could use a recursive WITH clause, or perhaps MODEL, which might be a little faster.

Saubhik

Your question is not clear enough. If you post CREATE table with INSERT statement for generating sample data and post the expected output, then someone may help to find a nifty way!

Deepak Mahto
Answer

As far as aware. might be wrong..

Regex_replace can't  replace multiple character with multiple character!!

but can replace multiple character with single character!!

SELECT REGEXP_REPLACE('COLLATERAL and AGREEMENT and VALUE','COLLATERAL|AGREEMENT|VALUE', 'REPLACE')

from dual;


output :

REPLACE and REPLACE and REPLACE

You can try something as below. (recursive with as mention by Frank Sir)

with alias1 as

(select rownum col1 , data1,data2

from (select 'COLLATERAL' data1, 'COLLAT' data2 from dual union all

select 'AGREEMENT'data1, 'AGREE' from dual union all

select 'VALUE'data1, 'VAL' from dual)) , --- get replace character

alias2 as

(select 'COLLATERAL and AGREEMENT and VALUE' col1 from dual),

r(str,lvl) as -- get complete string for replacement.

(

select REPLACE(col1,'COLLATERAL','COLLAT') str, 1 lvl  from alias2

UNION ALL

SELECT REPLACE(str,alias1.data1,alias1.data2) str, lvl + 1

from alias2,r,alias1

where alias1.col1 = r.lvl

)

select str from r where lvl = (select max(lvl) from r);

output :

COLLAT and AGREE and VAL

Hope it helps!

Marked as Answer by 1932140 · Sep 27 2020
1932140

Hi

Thanks for showing the difference between the two. I thought the former could  "replace multiple character with multiple character!!", so I guess I'm stuck with multiple REPLACE (s) functions.

Peter

1 - 4
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Dec 28 2014
Added on Nov 28 2014
4 comments
1,415 views