Sam_dia


Hi

do you know incase a script which extracts domain names from an email address

like foreg. we have xyz@dmc.com or abce@dmc-int.com

should give

dmc and

dmc-int





Re: How to extract domain name from an email address??

Gert-Jan van der Kamp


declare @email varchar(50),

@domain varchar(50)

set @email = 'johndoe@whatever.com'

set @domain = substring(@email, charindex('@', @email)+1, 50)

print @domain

Regards Gert-Jan







Re: How to extract domain name from an email address??

Christian_B

Close, but that still includes .com

Code Snippet

declare @email varchar(50),

@domain varchar(50)

set @email = 'johndoe@whatever.com'

set @domain = substring(@email, charindex('@', @email)+1, charindex('.', @email) - (charindex('@', @email) + 1))

print @domain

Chris






Re: How to extract domain name from an email address??

Gert-Jan van der Kamp

Ok, then drop the part to the right of the last dot:

declare @email varchar(50),

@domain varchar(50)

set @email = 'johndoe@nl.eu.whatever.com'

set @domain = substring(@email, charindex('@', @email)+1, 50)

set @domain = substring(@domain, 1, len(@domain) - charindex('.', reverse(@domain)))

print @domain

This gets the subdomains as well, if you only want the top domain you'll have to do sonthing similar to get the bit after the last dot.

Regards Gert-Jan






Re: How to extract domain name from an email address??

Christian_B

Yep, that works. Could do it in one step if needed, but it is probably less efficient

Code Snippet
set @domain = substring(@email, charindex('@', @email)+1, (len(@email) - charindex('.', reverse(@email))) - charindex('@', @email))

Chris





Re: How to extract domain name from an email address??

Jon A.

If you want to get fancy, you could try something like this:

http://blogs.msdn.com/sqlclr/archive/2005/06/29/regex.aspx





Re: How to extract domain name from an email address??

Arnie Rowland

This 'should' handle just about any eMail address you can come up with.


DECLARE @eMail varchar(100)
SET @eMail = 'john.doe@nl.eu.whatever.com'
SELECT parsename( substring( @eMail, ( charindex( '@', @eMail )), len( @eMail )), 2 )

(This darn #$%#^* editor overrides our code formatting. No code above should be underlined -or blue for that matter!)