Solved

Access / SQL SELECT query - "WHERE NOT LIKE"

Posted on 2006-06-20
4
462 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

624 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