How to write NOT LIKE subquery with values pulled from table? with wild cards

Hi Expert,

There 2 tables emailinfo(id,email) and keywordinfo(id,keyword). emailinfo contains 80,000 rows and keywordinfo contains 2000 rows.

I want emails from emailinfo table which does not contains keywords from keywordinfo table. eg. SELECT email FROM emailinfo WHERE email not like '%yahoo%' AND email not like '%gmail%'

but this is hardcoded example.and i kept these keywords in keywordinfo table.

In sql procedure i tried for following query,

SELECT EMAIL.email FROM emailinfo EMAIL WHERE ( not exists( Select keyword from keywordinfo where EMAIL.email like '%'+ keyword +'%' )) which take 2 minutes to execute.

I want query like following

SELECT EMAIL.email FROM emailinfo EMAIL where EMAIL.email not like '%' ( SELECT keyword from keywordinfo ) '%'

i know above subquery returns multple rows,but i want something like that.

i have googled many examples but they failed to satisfying my problem

Please help me to write NOT LIKE subquery with values pulled from another table?

Thanks in advance.

more ▼

asked May 17, 2012 at 09:43 AM in Default

avatar image

0 1 1 1

Better would have been a breakdown of the email column into actual email, keyword like "google . com". That way this query could have been handled efficiently.

May 17, 2012 at 09:55 AM robbin

Thanks for replying me. In my software there are 2 options during fetching emails 1 skip the email containig keywords 2 dont skip email containing keywords.

May 17, 2012 at 10:41 AM Uday
(comments are locked)
10|1200 characters needed characters left

1 answer: sort voted first

I can understand what is desired. You need to know any new keywords missing in your keyword table. But I guess this should be done at the time of insert/update if to be handled efficiently. Otherwise, should be done as a nightly job.

How about

Extract the keyword by finding the string between @ and first . DECLARE @email VARCHAR(50)

 SET @email = 'abc@gmail.com'

 SELECT LEFT(DomainName, CHARINDEX('.', DomainName) - 1 )
 SELECT RIGHT(@email, LEN(@email) - CHARINDEX('@', @email)) DomainName
 ) A

A very good article by Aaron Bertrand is here



I have simulated the exact case on my machine but without the use of NOT LIKE and it worked great

 SELECT [Id], LEFT(DomainName, CHARINDEX('.', DomainName) - 1 ) keyword, [EmailAddress]
 SELECT Id, RIGHT(EmailAddress, LEN(EmailAddress) - CHARINDEX('@', EmailAddress)) DomainName, [EmailAddress]
 FROM [dbo].[EmailAddress]
 ) A
 ) B
 LEFT JOIN [dbo].[keyword] AS K
 ON B.keyword = [K].[keywords]
more ▼

answered May 17, 2012 at 10:17 AM

avatar image

1.7k 1 5 10

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.



asked: May 17, 2012 at 09:43 AM

Seen: 1043 times

Last Updated: May 17, 2012 at 11:02 AM

Copyright 2018 Redgate Software. Privacy Policy