Avatar of davidcahan
davidcahan
Flag for United States of America

asked on 

How To Get Extension in an Email Address

I'm using the following function.  I need a part that grabs the extension (the part after the dot) so that i can compare that to all extensions that actually exist.

but of course it needs to be good enough not to get fooled by dcahan@ya.hoo.com .  In that example only "com" should come back.

can someone help me get the extension into a local variable using the code below as a starting point.
alter function dbo.IsValidEmail2(@email as varchar(500))
returns tinyint
as 
begin
 declare @res tinyint
 set @res = 0
 if (
      CHARINDEX(' ',LTRIM(RTRIM(@email))) = 0 
AND        LEFT(LTRIM(@email),1) <> '@' 
AND        RIGHT(RTRIM(@email),1) <> '.' 
AND        CHARINDEX('.',@email,CHARINDEX('@',@email)) - CHARINDEX('@',@email) > 1 
AND        LEN(LTRIM(RTRIM(@email))) - LEN(REPLACE(LTRIM(RTRIM(@email)),'@','')) = 1 
AND        CHARINDEX('.',REVERSE(LTRIM(RTRIM(@email)))) >= 3 
AND        (CHARINDEX('.@',@email) = 0 AND CHARINDEX('..',@email) = 0) 
and       @Email not like '%!%'
and       @Email not like '%#%'
and       @Email not like '%$%'
and       @Email not like '%^%'
and       @Email not like '%&%'
and       @Email not like '%*%'
and       @Email not like '%(%'
and       @Email not like '%)%'
and       @Email not like '%=%'
and       @Email not like '%+%'
and       @Email not like '%{%'	
and       @Email not like '%}%'	
and       @Email not like '%[%'	
and       @Email not like '%]%'	
and       @Email not like '%\%'	
and       @Email not like '%;%'	
and       @Email not like '%:%'	
and       @Email not like '%"%'	
and       @Email not like '%<%'	
and       @Email not like '%>%'	
and       @Email not like '%?%'	
and       @Email not like '%/%'	
)  set @res = 1 
  return @res  
end

Open in new window

Microsoft SQL Server 2005

Avatar of undefined
Last Comment
davidcahan

8/22/2022 - Mon