简体   繁体   English

无法使用短日期过滤MS Access Datetime字段

[英]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 <" 假设存在多个具有1/1/2014日期的记录,则查询不会产生任何记录,除非我复制存储在access中的实际日期时间数据或使用“>或<”

is there any way that i can format in my SQL query the datetime field to short date for easy comparison? 我有什么办法可以在SQL查询中将datetime字段格式化为短日期以便于比较? 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# . 但是由于我不知道如何或是否可以在Crystal Reports中使用Access参数查询,因此建议您将日期值格式设置为#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. 如果您想在将[datetime field]y天匹配时忽略一天中的时间,则可以使用DateValue([datetime field])来获取该日期的午夜。

"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. 但是,如果您的表包含许多行,则您不想为每一行都评估DateValue Instead you can do something like this, and it can utilize indexed retrieval for faster performance if [datetime field] is indexed. 相反,您可以执行类似的操作,并且如果对[datetime field]索引,它可以利用索引检索来提高性能。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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