Solved

Using LIKE with IN

Posted on 2004-04-26
5
267 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

708 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

12 Experts available now in Live!

Get 1:1 Help Now