Character Replace in Oracle SQL

In my sql query I am pulling all the email address from my company; example john.smith@abc.com
We are changing our domain to john.smith@hospital.org

Want to know if there is a way for me to search for the string @abc.com and replace it with @hospital.org in my query?
metalteckAsked:
Who is Participating?
 
Doug WaltonConnect With a Mentor Database AdministratorCommented:
Just to add to what was said already, it would end up looking something like this:

select <email column>, REPLACE(<email column>,'@abc.com','@hospital.org') from <table name> where <email column> like '%@abc.com';
update <table name> set <email column> = REPLACE(<email column>,'@abc.com','@hospital.org') where <email column> like '%@abc.com';

Open in new window

0
 
johnsoneSenior Oracle DBACommented:
The REPLACE function should do what you need.
0
 
slightwv (䄆 Netminder) Commented:
First:  You should ask Oracle questions in the Oracle Database Topic Area.  Some of us don't monitor other areas and can easily miss questions.  I've changed the Topic Areas for you.

You really don't provide enough information.

Do you have stored procedures/functions/packages that have that email address in them that you want to replace?
Do you have a folder in some OS that has a lot of scripts that you wish to replace?
What?

If in Oracle code, you can use a query against user_source to find the code that has the domain:
select name, type from user_source where lower(text) like '%@abc.com%';

Then you can use dbms_metadata.get_ddl to extract the actual DDL, to a simple replace with your favorite editor and re-execute the DDL.
0
 
slightwv (䄆 Netminder) Commented:
>>it would end up looking something like this:

The assumption is they are using a single email address column in a table or series of tables.  The problem is more involved if there is the possibility of more than one column in a table that might contain the email address.

It also assumes @abc.com is the entire end of the field.  If you are updating a column where the email address can exist in blocks of text, the above statements won't work.  You will need to double-ended wildcard in my example.
1
 
slightwv (䄆 Netminder)Connect With a Mentor Commented:
I used it in my example but thought I should make sure I mention it:  The REPLACE commands mentioned don't account for the case of the email addresses.  You might need to use the UPPER or LOWER functions to make sure you find everything and again with the replace to make sure you replace everything.

So, Doug's update statement will be:
update <table name> set <email column> = REPLACE(lower(<email column>),'@abc.com','@hospital.org') where lower(<email column>) like '%@abc.com';

Open in new window

1
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.