A Coder
A Coder

Reputation: 3046

Extract extension from email address in SQL

I wanted to extract the extension from email address.

Input: [email protected]
Output: com

Input: [email protected]
Output: test.com

I tried,

(REVERSE(LEFT(REVERSE('[email protected]'), CHARINDEX('.', REVERSE('[email protected]')) - 1))) 

This works only the first input. Any help?

Upvotes: 1

Views: 130

Answers (1)

Thom A
Thom A

Reputation: 95561

It seems you want to remove any characters prior to and including the first period (.) after the at symbol (@). I would use CHARINDEX and STUFF for this:

SELECT STUFF(V.Email,1,CHARINDEX('.',V.Email,CHARINDEX('@',V.Email)),'')
FROM (VALUES('[email protected]'),
            ('[email protected]'))V(Email);

Upvotes: 2

Related Questions