Solved

Access / SQL SELECT query - "WHERE NOT LIKE"

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

734 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