Solved

Using LIKE with IN

Posted on 2004-04-26
5
268 Views
Last Modified: 2009-07-29
Experts,

I have a query of the form

Select Field1 from T1
where Field1 not in
(Select Excluded_Values from T2)

I want the T2 table values to be wildcard-matched against Field1 (i.e. for the IN match to be a LIKE). LIKE creates an error. Anyone know how I do this?
0
Comment
Question by:metalaureate
5 Comments
 
LVL 34

Expert Comment

by:arbert
ID: 10924986
I guess we need a little more to go on....Like, like you said is usually used with a wild card--how many characters do you want to match????  
0
 

Author Comment

by:metalaureate
ID: 10925002
I have  list of IP addresses to exclude, i.e.

66.180.5.%
128.1.4.%

etc.

I want to exclude all records  that match these IP masks.

The problem arose when my list of excluded IP addresses grew too large for concaternations of (AND NOT LIKE '66.180.5.%') etc. etc.
0
 
LVL 6

Assisted Solution

by:jchopde
jchopde earned 200 total points
ID: 10925119
you could probably do something like

SELECT T1.Field1 FROM T1 WHERE T1.Field1 NOT IN (SELECT T1.Field1 FROM T1,T2 WHERE T1.Field1 LIKE '%' + T2.Excluded_Values)

If Excluded_Values will not contain the '%' in the database, you would add that to the end of the query.
0
 
LVL 39

Accepted Solution

by:
appari earned 300 total points
ID: 10925164
try something like this

Select Field1 from T1
where not exists
(Select Excluded_Values from T2 where Field1 like Excluded_Values )

0
 

Author Comment

by:metalaureate
ID: 10925276
Thanks. You got it.

Both jchopde and appari's answer achieve the same results, but jchopde's solution is 600% faster. Anyone know why?
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to shrink a transaction log file down to a reasonable size.

914 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now