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...
Continue Reading This Article
Enjoy this article as well as all of our content, including E-Guides, news, tips and more.
e-mail address like [email protected] That e-mail needs to be changed to [email protected] 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 ('[email protected]'); commit; select substr(email_address,1,instr(email_address,'@')) || 'yourcompany.com' as new_email_address from email_addresses; NEW_EMAIL_ADDRESS --------------------------------------------- [email protected]
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 --------------------------------------------- [email protected]
Also notice, the REPLACE function replaced every occurrence of 'my' in the string with 'your'.