Email Validation as part of SQL query

masplin

Active Member
Joined
May 10, 2010
Messages
413
Hi

I came across this thread on validating email addresses in SQL and wondered if i could use the same to remove invalid email addresses before importing to the data model.

sql server - TSQL Email Validation (without regex) - Stack Overflow

My current query is

Code:
select   distinctINVNUM,Sysdate,Invdate,
Depot,
Customer,
"GROUP",
CTSalut,CTFirstn,CTSurname,Name1,
Addr11,Addr12,Addr13,Addr14,
Pcode1,
Phone1,
CTPhone,
CTMobile,
CTEmail,
Advert,TType,CUSREC,
RegNum,DVMake,DVModel,DVDate,MOTDue,ServiceDue,CTCampaign,CTCampRef
from dbo.VW_InOutStock
where invnum<>'07C38213'
and
invdate<'2015-07-01'

but just adding
Code:
ANDWHERE CTEmail LIKE '%_@__%.__%' 
    AND PATINDEX('%[^a-z,0-9,@,.,_]%', REPLACE(CTEmail, '-', 'a')) = 0

says incorrect syntax. Also I don't want to throw out the entire record which I think this would do, but just the CTEmail field to be blank.

any suggestions as how to achieve this appreciated as don't think DAX is as powerful finding these types of errors.

Mike
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Code:
SELECT
distinctINVNUM
,Sysdate
,Invdate
,Depot
,Customer,
"GROUP",
CTSalut,CTFirstn,CTSurname,Name1,
Addr11,Addr12,Addr13,Addr14,
Pcode1,
Phone1,
CTPhone,
CTMobile,
CTEmail,
Advert,TType,CUSREC,
RegNum,DVMake,DVModel,DVDate,MOTDue,ServiceDue,CTCampaign,CTCampRef
FROM dbo.VW_InOutStock
WHERE invnum<>'07C38213'
AND invdate<'2015-07-01'
AND (CTEmail LIKE '%_@__%.__%' 
    AND PATINDEX('%[^a-z,0-9,@,.,_]%', REPLACE(CTEmail, '-', 'a')) = 0)

I THINK
 
Upvote 0
Will this exclude the complete record if the criteria isn't met? I'm guessing so as the other where clauses do that. Seems I need something that replaces these emails with a blank if they are found. Is that possible on import?
 
Upvote 0
Power Query would also be able to do this job I think.
If you're interested, please tell & I will put sth together here.
 
Upvote 0
Sorry very late responding on this but got side tracked. your code works with the only downside being it also removed every line where CTEmail is just NULL. I tried all sorts of combinations putting OR in but none work. Any iea how to apply the AND clauses, but leave in the blank ones?

Thanks
Mike
 
Upvote 0

Forum statistics

Threads
1,224,144
Messages
6,176,648
Members
452,739
Latest member
SCEducator

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top