Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace all email addresses after @ in Sql

I have a list of emails. I want to change all of them to test emails for my test system e.g. [email protected] to [email protected]. I don't want to use actual emails as it will create problems. Is it possible to change all emails at once in a single query or stored procedure.

like image 560
Abdul Avatar asked Aug 21 '15 06:08

Abdul


People also ask

How do I change email address in SQL?

Step 1: Launch SQL Server Management Studio and connect to the SQL Server instance where you want to change the details of a mail account. Expand the Management folder, go to Database Mail and right click, then select "Configure Database Mail". A list of options will appear as shown in the below screenshot.

How replace all occurrences of a string in SQL?

SQL Server REPLACE() Function The REPLACE() function replaces all occurrences of a substring within a string, with a new substring. Note: The search is case-insensitive.


2 Answers

I tried this and it worked perfectly.

 UPDATE myTable  SET UserEMail = 
 (SELECT SUBSTRING(UserEMail, 0, PATINDEX('%@%',UserEMail)) + '@example.org' 
 from myTable U WHERE U.UserID = myTable.UserID)
like image 93
Abdul Avatar answered Nov 16 '22 04:11

Abdul


This one works for me:

UPDATE [TABLE] [EMAIL] = REPLACE([EMAIL], '@company.com', '@test.com')

like image 27
Orlando Reynoso Avatar answered Nov 16 '22 03:11

Orlando Reynoso