tags:

views:

47

answers:

2

How do I specify a date range in MS Access? Is the below query correct? Do I have to put "2/1/2010" in quotes? Or do I have to do something like date(2/1/2010)?

SELECT [Occurrence Number] as Fld
  FROM [Lab Occurrence Form]
 WHERE [Practice Code]="ACCIM"
   AND [1 0 Preanalytical (Before Testing)]="1.1 Specimen Mislabeled"
   AND ([Occurrence Date] Between 2/1/2010 and 2/28/2010);

the following gives me a type mismatch

SELECT [Occurrence Number] as Fld FROM [Lab Occurrence Form] WHERE [1 0 Preanalytical (Before Testing)]="1.1 Specimen Mislabeled" AND [Occurrence Date] between "1/1/2009" and "2/2/2010";
+5  A: 

ms-access use the Jet engine which uses # for date literal:

SELECT Orders.*
  FROM Orders
 WHERE Orders.OrderDate Between #3/1/96# And #6/30/96#;
Alex W
success!!!!!!!!! you are the FREAKING MAN!!!!!! YEAH BABY!!!!!!!!!!!!
i am a girl
You can learn more about the Jet engine at:http://technet.microsoft.com/en-us/library/cc966377.aspx
Alex W
no thanks i hate JET engines
i am a girl
It is usually much better to use year, month, day. Month, day, year will usually suit Access, but it can get very confusing in locales other than the US.
Remou
I meant to learn about the SQL flavor used by the Jet engine.
Alex W
no thanks i hate JET engines
i am a girl
If you don't like Jet/ACE, then why are you using it? If you're not willing to learn how it works, why are you using it?
David-W-Fenton
+2  A: 
AND ([Occurrence Date] Between #2/1/2010# and #2/28/2010#

This is how you tell Access, to interpret something as date time.

shahkalpesh
It is usually much better to use year, month, day. Month, day, year will usually suit Access, but it can get very confusing in locales other than the US.
Remou
The key is that you need an unambiguous format, as Jet/ACE will interpret 3/4/2010 as a US data, no matter your local date settings. You might think yo're asking for 3 April, 2010, but Jet/ACE will think you're asking for March 4th, 2010. So, use DateSerial() or a date format that is unambigous (#1-Feb-2010#, for instance).
David-W-Fenton