Solved

Where clause doesn't work on date field

Posted on 2012-03-18
10
522 Views
Last Modified: 2012-03-18
Dear all experts,  I've a table which contains some a date field, when I run a query it cannot returns the value that I want, is there any thing wrong in my query?

Select * from Q_Analystz_Report where Type = 'C' contract_date > #01/03/2012#

I expect the above query can return the records which is larger than 1 Mar 2012 (record should up to 17 Mar 2012) but only one record returns.

Please note, the field is in DD/MM/YYYY format
0
Comment
Question by:towo2002
  • 6
  • 4
10 Comments
 
LVL 13

Expert Comment

by:Ashok
ID: 37734880
According to Microsoft

http://office.microsoft.com/en-us/access-help/where-clause-HA001231526.aspx

When you specify the criteria argument, date literals must be in U.S. format, even if you are not using the U.S. version of the Microsoft Access database engine. For example, May 10, 1996, is written 10/5/96 in the United Kingdom and 5/10/96 in the United States.

So try.....

Select * from Q_Analystz_Report where Type = 'C' contract_date > #03/01/2012#

HTH
Ashok
0
 
LVL 1

Author Comment

by:towo2002
ID: 37734892
Dear Ashok,

Thanks for your quick reply.  Is there anyway to use UK / Australia format?
0
 
LVL 13

Expert Comment

by:Ashok
ID: 37734893
By the way.....

To find records dated May 10, 1996 in a United Kingdom database, you must use the following SQL statement:
SELECT * FROM Orders WHERE ShippedDate = #5/10/96#;

You can also use the DateValue function which is aware of the international settings established by Microsoft Windows®. For example, use this code for the United States:
SELECT * FROM Orders WHERE ShippedDate = DateValue('5/10/96');

And use this code for the United Kingdom:
SELECT * FROM Orders WHERE ShippedDate = DateValue('10/5/96');

HTH
Ashok
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 13

Expert Comment

by:Ashok
ID: 37734900
Is there anyway to use UK / Australia format?

See my post above.

Ashok
0
 
LVL 1

Author Comment

by:towo2002
ID: 37734905
Dear Ashok,

Another question, how to identify the date format (or regional settings) for my access database?  I'm using English Windows 7 and regional settings is English Australia (date format is DD/MM/YYYY)

I'm using Access 2010.
0
 
LVL 13

Accepted Solution

by:
Ashok earned 300 total points
ID: 37734913
If regional setting is DD/MM/YYYY, DateValue function should work.

Try.....(copy & paste from below)

Select * from Q_Analystz_Report where Type = 'C' contract_date > DateValue('01/03/2012')

HTH
Ashok
0
 
LVL 1

Author Comment

by:towo2002
ID: 37734931
Sorry Ashok, one more question.  If I pass this query from VBA / VBScript, I still have to have to use US format?
0
 
LVL 13

Expert Comment

by:Ashok
ID: 37735052
I think you will have to use US format if you use SQL from VBA.
Query to Access is handled by Access Engine no matter how you pass it.

I cannot say it for sure.  Only way to verify how it will behave is to try it from VBA.

HTH
Ashok
0
 
LVL 1

Author Comment

by:towo2002
ID: 37735148
Just did try, have to use US format ;-(
0
 
LVL 13

Expert Comment

by:Ashok
ID: 37735396
US format with # signs.

You can use DateValue function in SELECT statement and you do not have to use US format (you know it by now).
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

856 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