简体   繁体   中英

Can't filter MS access datetime field using short date

I am trying to generate a crystal report for my program, apparently I wasn't able to filter those data which is in datetime format if i use "=" and not "> or <". my code goes something like this

dim x as string ="1/1/2014"
dim y as date
y=cdate(x)

select from [tblname] WHERE [datetime field]=#" & y &"#

the query won't yield any record assuming there is more than one record with 1/1/2014 date not unless i copy the actual datetime data stored in access or use the "> or <"

is there any way that i can format in my SQL query the datetime field to short date for easy comparison? because i believe the time stored is the reason why i can't filter it using "="

This would be better as a parameter query. But since I don't know how or if you can use an Access parameter query with Crystal Reports, I'll suggest you format the date value as #yyyy-md# .

"select from [tblname] WHERE [datetime field]=" & Format(y, "\#yyyy-m-d\#")

If you want to ignore time of day when matching [datetime field] to day y , you can use DateValue([datetime field]) which gives you midnight of that date.

"select from [tblname] WHERE DateValue([datetime field])=" & Format(y, "\#yyyy-m-d\#")

However, if your table contains many rows, you don't want to evaluate DateValue for every row. Instead you can do something like this, and it can utilize indexed retrieval for faster performance if [datetime field] is indexed.

"select from [tblname] WHERE [datetime field]>=" & Format(y, "\#yyyy-m-d\#") & " AND [datetime field]<" & Format(y + 1, "\#yyyy-m-d\#")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM