Nikhil
Nikhil

Reputation: 13

How to get only domain name from email ID in SQL Server

I have email ID's in a column, I want to get only domain name.

For example: I have [email protected] or [email protected] and I want only hp-dell, lenovo.

Can someone please help with this in SQL Server?

Upvotes: 0

Views: 579

Answers (3)

ScareCrowZV
ScareCrowZV

Reputation: 1

Make a directory from the top levels of domains - https://en.wikipedia.org/wiki/List_of_Internet_top-level_domains Then solve your problem through the REPLACE() function.

Upvotes: 0

Avinash Mane
Avinash Mane

Reputation: 1

Try this

declare @email nvarchar(200)
set @email = '[email protected]'
declare @startIndex int
declare @endIndex int
select @startIndex = charindex('@', @email, 1) + 1
select @endIndex = len(@email) - (charindex('.', reverse(@email) , 1) -1)


print @startIndex
print @endIndex
print substring(@email, @startIndex, @endIndex - @startIndex) -- will print testmine.abc

Upvotes: 0

SQLpro
SQLpro

Reputation: 5103

SELECT SUBSTRING(@email, 
                 CHARINDEX('@', @email) + 1, 
                 LEN(@email) - CHARINDEX('@', @email) - CHARINDEX('.', REVERSE(@email)))

Upvotes: 1

Related Questions