Ask the Expert

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?

    Requires Free Membership to View

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

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

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: