Solved

Access / SQL SELECT query - "WHERE NOT LIKE"

Posted on 2006-06-20
4
448 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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.

860 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