Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select domain name from email address

Tags:

mysql

I have email addresses like [email protected], [email protected] [email protected] ... etc. I want a Mysql SELECT that will trim user names and .com and return output as gmail,ymail,hotmail, etc.

like image 877
Ugesh Gali Avatar asked Apr 13 '10 08:04

Ugesh Gali


People also ask

How do I select a domain name from an email address in SQL?

LENGTH(email) - (INSTR(email, '@') + 1) - LENGTH(SUBSTRING_INDEX(email,'. ',-1)) will get the length of the domain minus the TLD (.com, . biz etc. part) by using SUBSTRING_INDEX with a negative count which will calculate from right to left.

What is domain name in email address example?

A domain name (often simply called a domain) is an easy-to-remember name that's associated with a physical IP address on the Internet. It's the unique name that appears after the @ sign in email addresses, and after www. in web addresses.

How do I extract domain from email in Excel?

To extract domain from email address Select a blank cell to place this formula =RIGHT(A2,LEN(A2)-FIND("@",A2)), press Enter key, and drag fill handle down to the cells which need this formula.


2 Answers

Assuming that the domain is a single word domain like gmail.com, yahoo.com, use

select (SUBSTRING_INDEX(SUBSTR(email, INSTR(email, '@') + 1),'.',1)) 

The inner SUBSTR gets the right part of the email address after @ and the outer SUBSTRING_INDEX will cut off the result at the first period.

otherwise if domain is expected to contain multiple words like mail.yahoo.com, etc, use:

select (SUBSTR(email, INSTR(email, '@') + 1, LENGTH(email) - (INSTR(email, '@') + 1) - LENGTH(SUBSTRING_INDEX(email,'.',-1))))  

LENGTH(email) - (INSTR(email, '@') + 1) - LENGTH(SUBSTRING_INDEX(email,'.',-1)) will get the length of the domain minus the TLD (.com, .biz etc. part) by using SUBSTRING_INDEX with a negative count which will calculate from right to left.

like image 165
anonymous Avatar answered Oct 18 '22 22:10

anonymous


I prefer:

select right(email_address, length(email_address)-INSTR(email_address, '@')) ... 

so you don't have to guess how many sub-domains your user's email domain has.

like image 22
Dan King Avatar answered Oct 18 '22 21:10

Dan King