Solved

I want to replace multiple matching patterns with multiple specified replacement strings

Posted on 2006-07-15
5
12,882 Views
Last Modified: 2007-12-19
Oracle 10g R1

We have varchar2(250) columns that have various text data we want to replace with other specific text.

We have record as string like:

123abc...""

and we want to replace each     &    and each     quot;

with 99 and 88 respectively.

I have tried REPLACE and REPLACE with CASE statements and we've tried REGEXP_REPLACE.
The problem seems that Oracle SQL will only replace the first one listed and ignores
any other 'find this and replace with this' after in the string.  

Here is an example of the SQL:

select
case
when "big_text_column" like '%&%' then replace("big_text_column",  '&'   ,    '99')
when "big_text_column" like '%quot;%'   then replace("big_text_column",  'quot;'     ,     '88')
   else "big_text_column"
   end as "big_text_column"
from table_with_big_text_column;

The result set 'fixes' the & ones but does nada for the quot; replacement;  
   123abc...99quot;99quot;

I would like it to return:
     123abc...99889988

How can I get it to change both (or more if needed) to their respective 99 and 88 ? We are not familiar with PL/SQL.
0
Comment
Question by:egarrison
  • 3
  • 2
5 Comments
 
LVL 14

Expert Comment

by:sathyagiri
Comment Utility
Try this
select replace(replace(big_text_column,'&amp','99'),'quot',88) from table_with_big_text_column;
0
 
LVL 14

Accepted Solution

by:
sathyagiri earned 500 total points
Comment Utility
Her's an example

SQL> set define off
SQL> select replace(replace('123abc...&ampquot;&ampquot','&amp','99'),'quot','88') from dual;

REPLACE(REPLACE('1
------------------
123abc...9988;9988
0
 
LVL 3

Author Comment

by:egarrison
Comment Utility
Thank you, thank you. I didn't expect and answer so soon. Works great.

A side note suppose I wanted to subsitute '&amp' with  a single quote?
Instead of 99
    select replace(replace('123abc...&ampquot;&ampquot','&amp','99'),'quot','88') from dual;

It returns '
    select replace(replace('123abc...&ampquot;&ampquot','&amp',' ' '),'quot','88') from dual;
 
 But the SQL errors at the 3 ' i row and the ESCAPE clause did not work I guess it is only
for wildcard characters and not reserved characters like single quote?
0
 
LVL 14

Expert Comment

by:sathyagiri
Comment Utility
Try this
select replace(replace('123abc...&ampquot;&ampquot','&amp',''''),'quot','88') from dual
0
 
LVL 3

Author Comment

by:egarrison
Comment Utility
Thank you, yep right on again...I guess I've been staring at this too long
as I had too many single quotes in my replacement sting for occurs of  '&amp'.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Article by: Swadhin
From the Oracle SQL Reference (http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries006.htm) we are told that a join is a query that combines rows from two or more tables, views, or materialized views. This article provides a glimps…
This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now