Solved

Access / SQL SELECT query - "WHERE NOT LIKE"

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

24 Experts available now in Live!

Get 1:1 Help Now