How to select domain name from email address

Mysql

Mysql Problem Overview


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.

Mysql Solutions


Solution 1 - Mysql

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.

Solution 2 - Mysql

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.

Solution 3 - Mysql

For PostgreSQL:

split_part(email, '@', 2) AS domain

Full query:

SELECT email, split_part(email, '@', 2) AS domain
FROM users;

Ref: http://www.postgresql.org/docs/current/static/functions-string.html

Credit to https://stackoverflow.com/a/19230892/1048433

Solution 4 - Mysql

Using SUBSTRING_INDEX for "splitting" at '@' and '.' does the trick. See documentation at http://dev.mysql.com/doc/refman/5.1/de/string-functions.html#idm47531853671216.

SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(email, '@', -1), '.', 1);

###Example:

SELECT SUBSTRING_INDEX(SUBSTRING_INDEX("[email protected]", '@', -1), '.', 1);

will give you "bar".

Here is what happens:

  • Split "[email protected]" at '@'. --> ["foo", "bar.buz"]
  • Pick first element from right (index -1). --> "bar.buz"
  • Split "bar.buz" at '.' --> ["bar", "buz"]
  • Pick first element (index 1) --> "bar"
    Result: "bar"

If you also need to get rid of subdomains, use:

SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(email, '@', -1), '.', -2), '.', 1);

###Example: SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX("[email protected]", '@', -1), '.', -2), '.', 1); will give you "bar".

Solution 5 - Mysql

If you want to know the most used domain names from email addresses you have (can be usefull), you can do :

select (SUBSTRING_INDEX(SUBSTR(email, INSTR(email, '@') + 1),'.',1)) as a,count(*) as c
FROM User
group by a
order by c desc;

Result :

enter image description here

Solution 6 - Mysql

Try this, removes the @ from the domain and just leaves the domain, example: domain.com

select SUBSTR(SUBSTR(email_field, INSTR(email_field, '@'), INSTR(email_field, '.')), 2) as domain

Solution 7 - Mysql

DECLARE @Email Varchar(20)
SET @Email='abc@gmail.com'
SELECT SUBSTRING(@Email, CHARINDEX('@',@Email)+1,LEN(@Email))

Solution 8 - Mysql

Try this:

select SUBSTR(field_name, INSTR(field_name, '@'), INSTR(field_name, '.'))

Solution 9 - Mysql

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

Some sql statements require the table name specified where the email column belongs to.

Solution 10 - Mysql

My suggestion would be (for mysql):

SELECT 
    LOWER(email) AS email,
    SUBSTRING_INDEX(email, '@', + 1) AS account,
 REPLACE(SUBSTRING_INDEX(email, '@', -1), CONCAT('.',SUBSTRING_INDEX(email, '.', -1)),'') -- 2nd part of mail - tld.
  AS domain,
    CONCAT('.',SUBSTRING_INDEX(email, '.', -1)) AS tld
FROM
...
ORDER BY domain, email ASC;

Solution 11 - Mysql

DECLARE @Email Varchar(20)
SET @Email='abc@gmail.com'
SELECT RIGHT(@Email,LEN(@Email)-CHARINDEX('@',@Email))

Solution 12 - Mysql

select SUBSTR(email, instr(email, '@') +1, instr(email,'.') -3)

exact answer is coming using this query

Solution 13 - Mysql

For MSSQL

declare @test as varchar(15) = 'foo@bar.buz'
SELECT SUBSTRING(@test, CHARINDEX('@', @test) + 1, LEN(@test)) AS ExtractString;

Solution 14 - Mysql

SELECT SUBSTR(NAME,INSTR(NAME,'@')+1) FROM ORACLE;

Oracle is my table.Don't be confuse.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionUgesh GaliView Question on Stackoverflow
Solution 1 - MysqlanonymousView Answer on Stackoverflow
Solution 2 - MysqlDan KingView Answer on Stackoverflow
Solution 3 - MysqlrattrayView Answer on Stackoverflow
Solution 4 - MysqlknugieView Answer on Stackoverflow
Solution 5 - MysqlJulienView Answer on Stackoverflow
Solution 6 - MysqlJoe L.View Answer on Stackoverflow
Solution 7 - MysqlSai VarunView Answer on Stackoverflow
Solution 8 - MysqlSarfrazView Answer on Stackoverflow
Solution 9 - Mysqluser4622420View Answer on Stackoverflow
Solution 10 - MysqlMr_KeyCodeView Answer on Stackoverflow
Solution 11 - MysqlSai VarunView Answer on Stackoverflow
Solution 12 - Mysqlrajeswari nolluView Answer on Stackoverflow
Solution 13 - MysqlMeluView Answer on Stackoverflow
Solution 14 - MysqlMayank AgrawalView Answer on Stackoverflow