Q

Using SQL to update e-mail addresses

I need to update e-mail addresses in our Oracle database. The update is supposed to do the following: I have an

e-mail address like firstname.lastname@cover.com. That e-mail needs to be changed to firstname.lastname@upper.com. Meaning the part after @ sign needs to be updated. Is there any function in SQL that could be used to make that update? To get the desired affect, we will need to use a combination of two functions, INSTR and SUBSTR, and a concatenation operator, the double pipe. INSTR tells us what position (base-1) the 'at' sign resides. The SUBSTR function allows us to grab just that portion up to and including the 'at' sign. And, of course, the concatenation operator lets us append our replacement string. Here's an example.

create table email_addresses 
( email_address varchar2(30)
);

insert into email_addresses 
  values ('myname@mycompany.com');
commit;

select substr(email_address,1,instr(email_address,'@')) ||
         'yourcompany.com' as new_email_address
  from email_addresses;

NEW_EMAIL_ADDRESS
---------------------------------------------
myname@yourcompany.com

If we know the text you wish to replace beforehand, we can use the REPLACE function.

select replace(email_address,'my','your') 
         as new_email_address
  from email_addresses;

NEW_EMAIL_ADDRESS
---------------------------------------------
yourname@yourcompany.com

Also notice, the REPLACE function replaced every occurrence of 'my' in the string with 'your'.

This was first published in October 2003

Dig deeper on Oracle and SQL

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide

SearchDataCenter

SearchContentManagement

SearchFinancialApplications

Close