tags:

views:

75

answers:

2

I have a email address like [email protected] and [email protected] [email protected] ... etc I want a Mysql select query so that it would trim user names and .com an returns output as gmail,ymail,hotmail etc

A: 

Try this:

select SUBSTR(field_name, INSTR(field_name, '@'), INSTR(field_name, '.'))
Sarfraz
A: 

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 .com, .biz part (not sure the exact term) by using SUBSTRING_INDEX with a negative count which will calculate from right to left.

Mr Roys
Thanks a lot worked for me :)
ugesh.gali
Hi ugesh gali, could you then click on the checkmark next to this answer to mark the question as answered? Thanks!
Mr Roys