tags:

views:

78

answers:

3

I am having trouble getting records out of a database for a certain date. This this the SQL command I have at the moment:

SELECT * FROM ict1 WHERE date='26/03/1992'

It should return one record from my database. "date" is a column and one of the records has '26/03/1992' as the value. I have tested the db connection and I can use:

SELECT * from ict1

So I know it's not that. It's probably just the SQL syntax being a lot different, I'm used to MySQL :@)

Should probably mention that I'm using .NET with an OleDbConnection.

Thanks.

A: 

Use the date as YYYY/MM/DD format:

SELECT * FROM ict1 WHERE date='1992/03/26'

SELECT * FROM ict1 WHERE date=#1992/03/26#

Randolph Potter
In Access, the date delimiter is hash, not quote.
Remou
Duly noted. Thanks.
Randolph Potter
+4  A: 

Usually dates need to be formatted as for access like the following

Select * from ict1 where date= #03/26/1992#

The # denotes a date in access. Be very careful with dates formatted as '10/03/1992' as it could mean 10th of march or 3rd of october depending on where you are.

Also #1992/03/26# also works

Nathan Fisher
It is much safer to use yyyy/mm/dd because it is unambiguous, both to Access and the user, no matter where your locale.
Remou
@Remou - I aggree. I have been caught out by the formatting of dates before.
Nathan Fisher
ISO format breaks in Access in certain contexts (though not this one), so the usual recommendation is to use a format like "d-mmm-yyyy" or some such, where the month is spelled out in letters instead of numbers. Frankly, I think it's a mistake to use Format() in the first place, and instead just pass dates with DateSerial(), which can never go wrong. Of course, that doesn't work via OLEDB, as in this context.
David-W-Fenton
A: 

You may want to use a date comparison function instead of date = #xxxx/xx/xx#. Date comparisons do not yield expected results because of formatting and data type issues. In SQL Server your date might be stored as a datetime or a date data type. You need to make sure you are comparing things in the same type and format.

DateDiff ("d", #1992/03/26#, date) = 0

mson