Can't filter MS access datetime field using short date

不想你离开。 提交于 2019-12-24 15:23:05

问题


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 "="


回答1:


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-m-d#.

"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\#")


来源:https://stackoverflow.com/questions/20863041/cant-filter-ms-access-datetime-field-using-short-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!