Home > Mobile >  Extract extension from email address in SQL
Extract extension from email address in SQL

Time:07-09

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?

CodePudding user response:

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