Mysql – Turn Emails into Usernames With MySQL Query


I am adding a username field to my database, and I want to populate it with the username portion of the email addresses the users have supplied…

I dont have many users, so I dont expect any conflicts.

What would a query look like to take the username portion of an email address and save it to the username column?

Thank you

Best Answer

I'd probably suggest doing it in the code that will be creating the insert statement but it can be done in the insert statement itself too.

INSERT INTO yourtable SET username = LOWER(SUBSTR(useremail,1,LOCATE('@',useremail) - 1))

I included the LOWER so the style of the user name would be consistent in case a person entered their email address in MixedCase.