63

I have email addresses like user1@gmail.com, user2@ymail.com user3@hotmail.com ... etc. I want a Mysql SELECT that will trim user names and .com and return output as gmail,ymail,hotmail, etc.

Andrew Brēza
  • 7,705
  • 3
  • 34
  • 40
Ugesh Gali
  • 2,042
  • 4
  • 20
  • 24

15 Answers15

86

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.

Bud Damyanov
  • 30,171
  • 6
  • 44
  • 52
anonymous
  • 3,474
  • 25
  • 29
  • 1
    Hi ugesh gali, could you then click on the checkmark next to this answer to mark the question as answered? Thanks! – anonymous Apr 13 '10 at 09:54
  • 1
    the term you're looking for is top-level-domain. and here's wikipedia for you: http://en.wikipedia.org/wiki/Top-level_domain – Devin May 08 '12 at 20:03
  • 38
    this part will return all characters after the '@': ``SUBSTR(email, INSTR(email, '@') + 1) `` – s2t2 Nov 21 '13 at 20:14
74

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.

Noel
  • 10,152
  • 30
  • 45
  • 67
Dan King
  • 841
  • 6
  • 2
22

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

Community
  • 1
  • 1
rattray
  • 5,174
  • 1
  • 33
  • 27
10

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("foo@bar.buz", '@', -1), '.', 1);

will give you "bar".

Here is what happens:
* Split "foo@bar.buz" 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("foo@1.2.3.bar.buz", '@', -1), '.', -2), '.', 1);

will give you "bar".

wteuber
  • 1,208
  • 9
  • 15
7

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

Julien
  • 3,743
  • 9
  • 38
  • 67
5

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
Joe L.
  • 4,433
  • 1
  • 19
  • 14
3
DECLARE @Email Varchar(20)
SET @Email='abc@gmail.com'
SELECT SUBSTRING(@Email, CHARINDEX('@',@Email)+1,LEN(@Email))
Sai Varun
  • 136
  • 1
  • 6
2

Try this:

select SUBSTR(field_name, INSTR(field_name, '@'), INSTR(field_name, '.'))
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
2

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.

1

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;
Mr_KeyCode
  • 31
  • 3
1
DECLARE @Email Varchar(20)
SET @Email='abc@gmail.com'
SELECT RIGHT(@Email,LEN(@Email)-CHARINDEX('@',@Email))
Frits
  • 7,341
  • 10
  • 42
  • 60
Sai Varun
  • 136
  • 1
  • 6
  • 1
    Please remember to use the code formatting functionality of the editor when posting code. Also, on a side note, is there a reason you didn't just edit your previous answer to add this to it as a secondary solution? And finally, please remember that a __good answer__ is often accompanied by an explanation of the code (why/how it was used). Reading and following through [How to answer](https://stackoverflow.com/help/how-to-answer) will help to drastically increase the quality of your answers. :) – Frits Jul 21 '17 at 12:37
  • Thanks @Frits. I'ma beginner. Thanks for your suggestions. How do i add a secondary solution to the same answer? How do i use code formatting functionality? – Sai Varun Jul 21 '17 at 13:00
  • Not a problem. At the bottom-left of your answer (before the comments) there is an _edit_ button, simply edit your answer to include your updated code. A line of code in your answer should always be preceded by 4 spaces - have a look at [Markdown Editing Help](https://stackoverflow.com/editing-help) :) – Frits Jul 21 '17 at 13:03
1

MYSQL:

SELECT mail,
        RIGHT(mail,LENGTH(mail)-LOCATE('@',mail)) as domain_name
        FROM email;
Ankit Jindal
  • 3,672
  • 3
  • 25
  • 37
Lekhnath
  • 11
  • 1
0

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

exact answer is coming using this query

Jayantha Lal Sirisena
  • 21,216
  • 11
  • 71
  • 92
  • 1
    Welcome to SO. Your answer does not appear to provide narrative or substantive difference from the accepted answer. Please, when providing an answer, explain why the code will help the OP (or someone with a similar question) and format your code. – jessi Mar 02 '20 at 15:45
0

For MSSQL

declare @test as varchar(15) = 'foo@bar.buz'
SELECT SUBSTRING(@test, CHARINDEX('@', @test) + 1, LEN(@test)) AS ExtractString;
Melu
  • 1,835
  • 3
  • 14
  • 13
-1
SELECT SUBSTR(NAME,INSTR(NAME,'@')+1) FROM ORACLE;

Oracle is my table.Don't be confuse.

sticky bit
  • 36,626
  • 12
  • 31
  • 42