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

Time:09-30

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?

CodePudding user response:

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

CodePudding user response:

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
  • Related