I should modify the existing email by discarding everything after # from the existing email address (or taking only the initial part of the email address to the # sign) and adding iths.edu.rs.
This one is wrong because it creates new email.
SELECT fname,lname,email,CONCAT(LOWER(fname),'.',LOWER(lname), '#iths.edu.rs') AS 'new_email'
FROM employee
WHERE
(LEFT(fname,1)='s' OR LEFT(lname,1)='s')
AND email IS NOT NULL
This is what I get
These are expected results
I think that you want to update the table:
UPDATE employee
SET email = CONCAT(LOWER(fname),'.',LOWER(lname), '#iths.edu.rs')
WHERE (LEFT(fname,1)='s' OR LEFT(lname,1)='s') AND email IS NOT NULL
I'm not sure about the condition:
(LEFT(fname,1)='s' OR LEFT(lname,1)='s')
but I left it there because you use it in your code.
I ended up using this to show only people whose emails have been changed. This was my task.
UPDATE employee
SET email = CONCAT(LOWER(fname),'.',LOWER(lname), '#iths.edu.rs')
WHERE (LEFT(fname,1)='s' OR LEFT(lname,1)='s') AND email IS NOT NULL;
SELECT fname,lname,email
FROM employee
WHERE (LEFT(fname,1)='s' OR LEFT(lname,1)='s') AND email IS NOT NULL
Related
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'
I have csv list which contains bounced emails addresses, one per line.
example1#example1.com
example2#example2.com
example3#example2.com
...
example700#example12.com
I need to change the user_state for each of these email to 'email_bounce'.
With the following SQL query i can do the changes using one email address at a time.
UPDATE xf_user SET user_state = 'email_bounce' WHERE email = 'example1#example1.com';
UPDATE xf_user SET user_state = 'email_bounce' WHERE email = 'example2#example2.com';
and so on
I made a copy of the DB where i would like to do a dry run but i have no idea how to automate this SQL query to go through all the emails in my csv.
I am using benkeens open source script (http://benkeen.github.io/generatedata/) to generate random data. The problem is it gives weird email domains within the database. The code I was using to change the domains is:
UPDATE emails
SET email_address = CONCAT(LEFT(email_address, INSTR(column, '#')), 'google.com')
WHERE email LIKE '%#magnamalesuada.org%';
The only issue is, there are literally a hundred different domains being generated. Is it possible to change domain names to google, yahoo, hotmail, etc in a more efficient way?
Assuming you have an id column, you could use the % operator to divide your rows in groups, for example in 3:
UPDATE emails
SET email_address = CONCAT(LEFT(email_address, INSTR(column, '#')), 'google.com')
WHERE id%3=0;
UPDATE emails
SET email_address = CONCAT(LEFT(email_address, INSTR(column, '#')), 'yahoo.com')
WHERE id%3=1;
UPDATE emails
SET email_address = CONCAT(LEFT(email_address, INSTR(column, '#')), 'hotmail.com')
WHERE id%3=2;
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 have a user update function and I allow users to change their email address but the same address must be unique in the database so, before I update, I must check if their new email already exists in the database but the query I use to check that returns the same row. Example:
user = User.query.get(1)
user.email = 'some#email.com'
if user.validate(): # The validate function performs a query to see if 'some#email.com' is already taken
user.save()
Now going into the validate function I have:
check = User.query.filter_by(User.email='some#email.com').first()
if check:
# email already exists
Problem is that check holds the same user I'm editing. Sqlalchemy submits the update to the database but under some sort of transaction so my query returns the same user I'm editing. I've solved this by creating a second session object but seems like an overkill. Any better ideas? Am I making sense?
Why not check, whether a user with a given e-mail address exists, before manipulating an existing user? You could e.g. write a standalone function for that:
def user_email_exists(email):
return (not User.query.filter(User.email=email) == None)
Then call user_email_exists before the attempt to change the user object.
...
# User object to alter
user = ...
# the new email address, which needs to be checked
new_email_addr = 'new#shiny.com'
if user_email_exists(new_email_addr):
raise SomeMeaningfulException() # or some `flash` message + a redirect
else:
user.email = new_email_addr
db.session.add(user)
db.commit()
...
The simplest way is probably just to add a second filter to the query inside your validate function. Instead of having it select any user with that email address, have it select any user with that email address and NOT the same username or user id. That way you're ensuring to only get a return if there is a different user with that email address.