we need to update our mail addresses in a table, because we have a new domain-part. The local-part remains untouched
In the table is a column for Name, Surname and Mail. (And other columns which are not important).
We want it to look like this in the end:
Name Surname Mail
Test Name Test.Name#newdomain.com
Test2 Name2 Test2.Name2#newdomain.com
But while trying to do so we broke it and now the mail column only shows the new domain. We used the following code:
update table
set mail = Replace('olddomain.com','newdomain.com')
where mail LIKE '%olddomain.com'
So now we need to restore the mail column and add the new domain-part. Any help?
I'm surprised this works. Normally, replace() takes three arguments:
set mail = Replace(mail, 'olddomain.com', 'newdomain.com')
I might suggest that you include the # in the logic as well.
replace() takes three arguments
update table
set mail = Replace(mail,'#olddomain.com','#newdomain.com')
where mail LIKE '%olddomain.com'
Related
I have a table with columns like:
emails, country_code etc
Some of the rows contain emails like:
XXXXX#googlemail.com
I want to change about 10,000 records like this in a way that will affect only the "googlemail.com" part of the value and change all of them to "gmail.com".
UPDATE exmple_table SET emails = REPLACE(emails, '%googlemail.com','%gmail.com');
I tried to find and replace but that making me have to type all 10,000 addresses in the query, any solutions?
You can use 'like' operator to filter out the records which contain 'googlemail' and then perform the string replace on them, as shown below:
update table
set SET emails = REPLACE(emails, 'googlemail.com','gmail.com')
where emails like '%googlemail.com%'
I have a database with a large set of email addresses.
Because of a bug in a script, the database is full of wrong email addresses. These addresses has a known pattern.
They are made of a true email address, concatenated with a string in the beginning.
This string is itself a part of the email address.
Example:
The correct email should be:
john.doe#example.com
Instead I have:
doejohn.doe#example.com
Or also:
johndoejohn.doe#example.com
How can I identify these addresses?
I thought about creating a regexp that finds repeating text inside a string, but I could find out how to do it.
Any ideas?
You can use below query to take care of LASTNAMEfirstname.lastname#something.com pattern, This will first find the last_name and then replace that with null in the first part before first ..
concat(replace(substr(email,1,locate('.',email)),substr(email,LOCATE('.',email)+1,locate('#',email)-LOCATE('.',email)-1),'')
,
substr(email,locate('.',email)+1,length(email))
)
See SQL Fiddle example here
http://sqlfiddle.com/#!9/24fba/2
But this will not take care of FIRSTNAMElastnameFIRSTNAME.lastname#example.com pattern.
Can't test right now but this might work:
^([^#]{5,})[^#]{1,}\.\1#[^#]+$
Recently I mass added customers details. However, my client just updated me that the email address format is wrong.
For example: The previous given email was abc#mail.com. However, I have to edit to this to abc#me.mail.com.
Is there any way I can check for #mail.com and change it to #me.mail.com?
update your_table
set email = replace(email, '#mail.com', '#me.mail.com')
where email like '%#mail.com'
I'm trying to build a query similar to the following, but am unsure if it will work:
UPDATE userbase SET EndDate='today' where Owner='me' OR Owner2='me'
I want to update one value where the person using it could be owner one or two, if that makes any sense.
Thank you!
That looks correct and should work as intended - assuming you have 2 fields, called Owner and Owner2 and want o check that either of them contains 'me'.
If you want to check if the Owner field contains either 'me' or 'me2' you'd do
... WHERE Owner = 'me' OR Owner = 'me2'
Oh and you may want to consider using the NOW() function and similar to set dates/times (In case you're not aware of them).
The statements you wrote looks good, it should be Ok.
For your ref: SQL UPDATE
If I have a list of of email accounts in a .txt file, is there a way I can perform a mysql delete statement for each instance of the rows that contain the email account against a table?
We have a newsletter mailing list which around 600 emails are currently invalid, and we want an easier way of getting rid of them besides manually going in one by one to do it.
Use excel to turn the dataset into a comma separated string and then simply:
DELETE FROM YOUR_TABLE
WHERE email IN ('example#aol.com', 'example2#aol.com')
Note that you will need to manually add the single quotes before and after each email address in excel as well.
Assuming you want to use the text file's contents as the source of addresses to delete from the database:
$addreses = file('emails.txt');
foreach($addresses as $address) {
$safe_address = mysql_real_escape_string($address);
$sql = "DELETE FROM yourtable WHERE (email = '$safe_address');";
$result = mysql_query($sql) or die(mysql_error());
}
is the simplest form. You can do some things to optimize the process, such as creating a list of emails in quoted/comma-separated form and using WHERE IN (...) instead, to reduce the number of queries generated/executed.
Note that I'm using PHP as a pseudo-code placeholder, but the basic principle would be the same in pretty much any language.
What about create the delete statement for single email then use Microsoft excel to complete the rest of the emails? by using the concatenate function and matching with the list of the selected emails