vaibhav shah
vaibhav shah

Reputation: 5069

Extracting Email Domain from string in sql

I have string something like this

  1. 'myname' <[email protected]>

or like this

  1. [email protected]

I want to extract domain from both kind of strings

For 2nd I can do somthing like this

select RIGHT(email, LEN(email) - CHARINDEX('@', email))

but same is not working for 1st string.

Also I do not know in which format email address will come

I am using Sql Server 2014.

Upvotes: 0

Views: 89

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1271211

You can use stuff() and replace():

select replace(stuff(email, 1, charindex('@', email), ''), '>', '')

Here is a db<>fiddle.

Upvotes: 3

Related Questions