dbTalk Databases Forums  

BETWEEN criteria not working correctly

comp.databases.ms-access comp.databases.ms-access


Discuss BETWEEN criteria not working correctly in the comp.databases.ms-access forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
emanning
 
Posts: n/a

Default BETWEEN criteria not working correctly - 07-08-2010 , 02:18 PM






I've got an Access 2007 table with a field called Zip which is a text
field. Some zips were entered like "67203" and others like
"67203-2752". Is there a way to format a criteria statement in a
query to pull all zips between "67203" and "67203" without having to
resort to something like between "67203" and "67203-9999" or between
"67203" and "67204"?


Thanks

Reply With Quote
  #2  
Old   
John Spencer
 
Posts: n/a

Default Re: BETWEEN criteria not working correctly - 07-08-2010 , 02:55 PM






You could use
LIKE "67203*"

If you want a range you are going to have to use something like
WHERE Zip >= "67203" AND Zip < "67204"

Or you can use the Val function and then number values (at a cost in
efficiency and speed)

WHERE Val(ZIP) Between 67203 and 67203

Or you can use (at a cost in efficiency and speed)
WHERE LEFT(Zip,5) Between "67203" AND "67203"


John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County

emanning wrote:
Quote:
I've got an Access 2007 table with a field called Zip which is a text
field. Some zips were entered like "67203" and others like
"67203-2752". Is there a way to format a criteria statement in a
query to pull all zips between "67203" and "67203" without having to
resort to something like between "67203" and "67203-9999" or between
"67203" and "67204"?


Thanks

Reply With Quote
  #3  
Old   
emanning
 
Posts: n/a

Default Re: BETWEEN criteria not working correctly - 07-08-2010 , 03:01 PM



On Jul 8, 2:55*pm, John Spencer <JSPEN... (AT) Hilltop (DOT) umbc> wrote:
Quote:
You could use
* *LIKE "67203*"

If you want a range you are going to have to use something like
WHERE Zip >= "67203" AND Zip < "67204"

Or you can use the Val function and then number values (at a cost in
efficiency and speed)

WHERE Val(ZIP) Between 67203 and 67203

Or you can use (at a cost in efficiency and speed)
WHERE LEFT(Zip,5) Between "67203" AND "67203"

John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County



emanning wrote:
I've got an Access 2007 table with a field called Zip which is a text
field. *Some zips were entered like "67203" and others like
"67203-2752". *Is there a way to format a criteria statement in a
query to pull all zips between "67203" and "67203" without having to
resort to something like between "67203" and "67203-9999" or between
"67203" and "67204"?

Thanks- Hide quoted text -

- Show quoted text -
I was just experimenting with 'WHERE LEFT(Zip,5) Between "67203" AND
"67203"' when I read your post. It appears to work just fine. Thanks
for your help.

Reply With Quote
  #4  
Old   
David W. Fenton
 
Posts: n/a

Default Re: BETWEEN criteria not working correctly - 07-09-2010 , 06:12 PM



John Spencer <JSPENCER (AT) Hilltop (DOT) umbc> wrote in
news:i15ag9$5de$1 (AT) news (DOT) eternal-september.org:

Quote:
You could use
LIKE "67203*"
It's a string field and likely indexed, so this seems to me without
question to be the correct approach, as well as the most efficient
(LIKE conditions that start with the beginning of the field are
usually able to utilize the index).

Quote:
If you want a range you are going to have to use something like
WHERE Zip >= "67203" AND Zip < "67204"
This would also work and would probably be the most efficient user
of the index.

Quote:
Or you can use the Val function and then number values (at a cost
in efficiency and speed)

WHERE Val(ZIP) Between 67203 and 67203

Or you can use (at a cost in efficiency and speed)
WHERE LEFT(Zip,5) Between "67203" AND "67203"
Do you have a typo here? Both tested values are the same?

I don't see any reason for using a function and then losing the
benefit of the indexes.

--
David W. Fenton http://www.dfenton.com/
usenet at dfenton dot com http://www.dfenton.com/DFA/

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.