views:

1123

answers:

5

A data import was done from an access database and there was no validation on the email address field. Does anyone have an sql script that can return a list of invalid email addresses (missing @, etc).

Thanks!

+3  A: 

Here is a quick and easy solution:

CREATE FUNCTION dbo.vaValidEmail(@EMAIL varchar(100))

RETURNS bit as
BEGIN     
  DECLARE @bitRetVal as Bit
  IF (@EMAIL <> '' AND @EMAIL NOT LIKE '_%@__%.__%')
     SET @bitRetVal = 0  -- Invalid
  ELSE 
    SET @bitRetVal = 1   -- Valid
  RETURN @bitRetVal
END

Then you can find all rows by using the function:

SELECT * FROM users WHERE dbo.vaValidEmail(email) = 0

If you are not happy with creating a function in your database, you can use the LIKE-clause directly in your query:

SELECT * FROM users WHERE email NOT LIKE '_%@__%.__%'

Source

Espo
+1 for the UDF.
Tomalak
+6  A: 
SELECT * FROM people WHERE email NOT LIKE '%_@__%.__%'

Anything more complex will likely return false negatives and run slower.

Validating e-mail addresses in code is virtually impossible.

EDIT: Related questions

Tomalak
I've used this one and it has not failed me in years. I consider myself pretty good at regexs but I think a cylon wrote this http://ex-parrot.com/~pdw/Mail-RFC822-Address.html
Chad Grant
Thanks, this is awesome! I did a search of stackoverflow but I should have used google search :/Cheers!
campo
Already too complex and wrong. foo@bar is a legal email address (providing the ".bar" TLD exists and has either an address or a MX record).
bortzmeyer
Calling this even "unlikely" would be very British already. The expression is not for validating e-mail addresses or checking every corner case. It is a basic sanity check that covers 99.9% of all cases without yielding false negatives, and I did not indicate otherwise.
Tomalak
Validating e-mail addresses with a regular expression (not for the faint of heart): http://www.regular-expressions.info/email.html (as you see, validating standard e-mail addresses is a LOT harder than validating common email addresses)
iconiK
foo@bar is going to crop up on the public Internet in the not too distant future too: http://www.canon.com/news/2010/mar16e.html
David Dorward
A: 

select * from users WHERE NOT ( 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)

A: 
select email 
from loginuser where
patindex ('%[ &'',":;!+=\/()<>]%', email) > 0  -- Invalid characters
or patindex ('[@.-_]%', email) > 0   -- Valid but cannot be starting character
or patindex ('%[@.-_]', email) > 0   -- Valid but cannot be ending character
or email not like '%@%.%'   -- Must contain at least one @ and one .
or email like '%..%'        -- Cannot have two periods in a row
or email like '%@%@%'       -- Cannot have two @ anywhere
or email like '%.@%' or email like '%@.%' -- Cant have @ and . next to each other
or email like '%.cm' or email like '%.co' -- Unlikely. Probably typos 
or email like '%.or' or email like '%.ne' -- Missing last letter

This worked for me. Had to apply rtrim and ltrim to avoid false positives.

Source: http://sevenwires.blogspot.com/2008/09/sql-how-to-find-invalid-email-in-sql.html

Manishm