We help IT Professionals succeed at work.
Get Started

How To Get Extension in an Email Address

214 Views
Last Modified: 2012-05-06
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

Comment
Watch Question
SQL SERVER EXPERT
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 2 Answers and 6 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE