Solved

Create a function

Posted on 2011-09-25
5
187 Views
Last Modified: 2012-05-12
How can I turn this select statement into a function that will return true if the email address appears valid and false if it doesn't. This select statement works, but only returns bad email addresses.

SELECT *  FROM ARCUSFIL_SQL WHERE NOT (
 	CHARINDEX(' ',LTRIM(RTRIM([Email_addr]))) = 0  AND
  	LEFT(LTRIM([Email_addr]),1) <> '@'  AND
  	RIGHT(RTRIM([Email_addr]),1) <> '.'  AND
  	CHARINDEX('.',[Email_addr],CHARINDEX('@',[Email_addr])) - CHARINDEX('@',[Email_addr]) > 1  AND
  	LEN(LTRIM(RTRIM([Email_addr]))) - LEN(REPLACE(LTRIM(RTRIM([Email_addr])),'@','')) = 1  AND 
 	CHARINDEX('.',REVERSE(LTRIM(RTRIM([Email_addr])))) >= 3  AND
  	(CHARINDEX('.@',[Email_addr]) = 0 AND CHARINDEX('..',[Email_addr]) = 0)  )

Open in new window

0
Comment
Question by:red_75116
  • 3
5 Comments
 
LVL 29

Expert Comment

by:QPR
ID: 36596785
Have you pasted the right bit? In the right forum?
0
 
LVL 29

Expert Comment

by:QPR
ID: 36596848
Here is the syntax for creating a function
http://msdn.microsoft.com/en-us/library/ms186755.aspx

Look at the "return" keyword - this will allow you to return true/false based upon your select statement within the function
0
 
LVL 29

Expert Comment

by:QPR
ID: 36596851
someones done one here
http://www.nimbleuser.com/blogs/Developers/developers/SQL_Function_to_Validate_email_address_syntax.aspx

It's basic but might do the trick if needed to be bulletproof
Failing that, google: t-sql validate email address
Bound to be heaps
0
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 500 total points
ID: 36598089
like this ?

C
REATE FUNCTION dbo.fnValidateEmail(@EMAIL varchar(100))
returns bit
as
Begin
return (
Select case when email like '_%@__%.__%'            -- basic valid pattern x@xx.xx
            then case when charindex(' ',email) = 0 -- no spaces
                       and left(email,1)<>'@'       -- not start with @
                       and left(liame,1)<>'.'       -- not end with .
                       and charindex('.@',email)=0  -- name cant end in .
                       and charindex('..',email)=0  -- cant have ..
                       and charindex('.',liame)>=3  -- suffix must be at least 2 char
            -- handled by pattern  period after @
            --         and CHARINDEX('.',[Email],CHARINDEX('@',[Email])) - CHARINDEX('@',[Email]) > 1
                       and LEN([Email]) - LEN(REPLACE([Email],'@','')) = 1 -- only 1 @
                      then 1
                      else 0
                      end
            else 0
            end
  from (select email,reverse(email) as liame
          from (select ltrim(rtrim(coalesce(@email,''))) as email) as x
       ) as y
)
End

Open in new window

0
 

Author Closing Comment

by:red_75116
ID: 36717922
Works perfectly!  Thanks!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question