Created
June 10, 2020 16:39
-
-
Save githubcom13/7bb60e0fb863de6151879e3f3f071336 to your computer and use it in GitHub Desktop.
Working with Email Addresses in SQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// QUERY - Checking if an email address if valid /// | |
SELECT email, | |
email REGEXP '^[A-Za-z0-9._%\-+!#$&/=?^|~]+@[A-Za-z0-9.-]+[.][A-Za-z]+$' AS valid_email | |
FROM email_table | |
/// RESULT /// | |
email | valid_email | |
---- | ---- | |
[email protected] | 1 | |
[email protected] | 1 | |
[email protected] | 1 | |
kramr.$@cofee_table^^ | 0 | |
/// QUERY - Splitting an email into local and domain names /// | |
SELECT email, | |
SUBSTRING_INDEX(email,'@',1) AS user_name, | |
SUBSTRING_INDEX(email,'@',-1) AS domain_name | |
FROM email_table | |
/// RESULT /// | |
email | user_name | domain_name | |
---- | ---- | ---- | |
[email protected] | jerry | gmail.com | |
[email protected] | gerge | vandalay.com | |
[email protected] | ebenes | tufts.edu | |
/// QUERY - Checking for consumer emails /// | |
SELECT e.email, | |
CASE WHEN c.domain IS NOT NULL THEN 'consumer' ELSE 'business' END AS email_type | |
FROM email_table e | |
JOIN consumer_domains c | |
ON c.domain = SUBSTRING_INDEX(email,'@',-1) | |
/// RESULT /// | |
email | email_type | |
---- | ---- | |
[email protected] | consumer | |
[email protected] | business | |
[email protected] | business |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment