Solved

Access / SQL SELECT query - "WHERE NOT LIKE"

Posted on 2006-06-20
4
439 Views
Last Modified: 2008-01-09
I have created 3 different SELECT queries within Access which are designed to split up a form based on the value in the Postcode column.

So for example one splits it up between postcodes beginning with HR1, HR2, HR3 and another anything beginning with WR, another SY etc. etc.

I now want to create a new query to show me all postcodes that do not begin with any of these values (Note, the above values are just examples, there's actually lots more).

Here's what I have for one of the original queries:

SELECT *
FROM UntouchedRecs
WHERE Postcode Like 'HR1*' Or Postcode Like 'HR2*' Or Postcode Like 'HR3*' Or Postcode Like 'HR4*' Or Postcode Like 'HR5*' Or Postcode Like 'CU*';

And here is my new query so far, which is using the NOT LIKE statement and combines the criteria from the previous 3 SELECT queries:

SELECT *
FROM UntouchedRecs
WHERE Postcode Not Like 'HR1*' Or Postcode Not Like 'HR2*' Or Postcode Not Like 'HR3*' Or Postcode Not  Like 'HR4*' Or Postcode Not  Like 'HR5*' Or Postcode Not Like 'CU*' Or Postcode Not Like 'SY*'  Or Postcode Not Like 'LD*' Or Postcode Not Like 'NP*' Or Postcode Not Like 'CF*' Or Postcode Not  LIKE 'WR*' OR Postcode Not  LIKE 'HR6*' OR Postcode Not LIKE 'HR7*' OR Postcode Not  LIKE 'HR8*' OR Postcode Not Like  'HR9*' OR Postcode Not  LIKE 'SY1 *' OR Postcode Not  LIKE 'SY2 *' OR Postcode Not  LIKE 'SY3 *' OR Postcode Not  LIKE 'SY4 *' OR Postcode Not  LIKE 'SY5 *' OR Postcode Not  LIKE 'SY7 *' OR Postcode Not  LIKE 'SY8 *' OR Postcode Not LIKE 'SY9 *' OR Postcode Not LIKE 'SY10 *' OR Postcode Not  LIKE 'SY11 *' OR Postcode Not  LIKE 'SY12 *' OR Postcode Not LIKE 'SY13 *' OR Postcode Not  LIKE 'SY14 *' OR Postcode Not LIKE 'SY15 *';


This does not work though - no records are eliminated from the table UntouchedRecs when running this query.
0
Comment
Question by:wbstech
  • 2
4 Comments
 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
ID: 16941154
If you don't want postcodes that begin any of these values, Use ANDs in place of the or's.
0
 
LVL 4

Author Comment

by:wbstech
ID: 16941182
Of course! Thanks.
0
 
LVL 61

Expert Comment

by:mbizup
ID: 16941188
Glad to help out!
0
 
LVL 16

Expert Comment

by:Rick_Rickards
ID: 16941244
In addition to replacing the OR's with AND's as correctly suggested by mbizup you can shorten the query a great deal by taking advantage of character Ranges ie: [1-5] (for all numbers 1 thru 5).  Example provided below using the query posted above to illustrate the idea.

SELECT *
FROM UntouchedRecs
WHERE (((UntouchedRecs.Postcode) Not Like 'HR[1-5]*' And (UntouchedRecs.Postcode) Not Like 'CU*' And (UntouchedRecs.Postcode) Not Like 'SY*' And (UntouchedRecs.Postcode) Not Like 'LD*' And (UntouchedRecs.Postcode) Not Like 'NP*' And (UntouchedRecs.Postcode) Not Like 'CF*' And (UntouchedRecs.Postcode) Not Like 'WR*' And (UntouchedRecs.Postcode) Not Like 'HR[6-9]*' And (UntouchedRecs.Postcode) Not Like 'SY[1-9] *' And (UntouchedRecs.Postcode) Not Like 'SY1[0-5] *'));

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

920 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

13 Experts available now in Live!

Get 1:1 Help Now