|
-
Apr 13th, 2007, 11:49 AM
#1
Thread Starter
Lively Member
date format mismatch
Hi,
I'm using access and my date field is formatted as shortdate - dd/mm/yyyy.
But when I try to query the table as below, there is a datatype mismatch.
Code:
SELECT *
FROM tablename
WHERE datefield='12/04/2007';
is it possible to format the date field in the sql statement or should I format the date value in the front end of the application and then pass this.
I would appreciate any help. Thanks in advance
-
Apr 13th, 2007, 11:54 AM
#2
Re: date format mismatch
That is because Access does not support single quotes for dates.
In Access, you need to enclose dates with the # sign.
-
Apr 13th, 2007, 11:55 AM
#3
Re: date format mismatch
For Access, Dates need to be surrounded by # instead of '
Much more importantly, dates in an SQL string will always be interpreted as MM/DD/YYYY unless they are blatantly meant to be different (such as 31/04/2007).
When putting dates into SQL strings you should format them to either MM/DD/YYYY or (better) YYYY/MM/DD
-
Apr 13th, 2007, 12:03 PM
#4
Thread Starter
Lively Member
Re: date format mismatch
Well I tried using hash as below
Code:
SELECT *
FROM tablename
WHERE datefield=#12/11/2007#;
but no data was returned in Access although the table had data for 12/11/2007. in the table though the value is displayed in dd/mm/yyyy format when i click on that field it becomes 12/11/2007 7:58:10 AM.
I'm passing this as a paramter from my front end in C#, since it kept throwing a datattype mismatch error 'm trying to make it work inside access.
-
Apr 13th, 2007, 12:06 PM
#5
Re: date format mismatch
It doesn't matter what the date format is in the table design (that is only used for Access forms etc), in SQL statements you must use the formats I suggested.
-
Apr 13th, 2007, 12:07 PM
#6
Thread Starter
Lively Member
Re: date format mismatch
Hmmm...ok.....but why isn't the query returning any data despite having matches in the table?? I'm using the format u suggested.
Last edited by sunshine123; Apr 13th, 2007 at 12:10 PM.
-
Apr 13th, 2007, 02:00 PM
#7
Re: date format mismatch
In that case I would assume that there is also a time in the field. If that is the case, the field will not be exactly the value you specified.
One way to solve it is to use Between with the time for the end of the day, eg:
Code:
WHERE date1 Between #04/13/2007# AND #04/13/2007 23:59:59#
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|