MS Access 按时间段查询数据

Access 总是有各种各样的坑。

比如数据查询需要按时间段进行查询,需要精确到秒:

查询一:(不能用)

select * from [YourTable] where [YourTimeField] >= #2014/04/04 14:24:03# and [YourTimeField] <= #2014/04/04 14:24:05#

看起来好像一切正常,但是实际返回的数据为空,不能用。

 

查询二:(不能用)

select * from [YourTable] where [YourTimeField] >= '2014/04/04 14:24:03' and [YourTimeField] <= '2014/04/04 14:24:05'

可能会因为系统配置的时间格式不同产生不同结果

 

查了半天资料,找到下面两种查询方式:

 

查询三:(OK)

select * from [YourTable] where DateDiff("s", [YourTimeField] ,#2014/04/04 14:24:03#) <= 0 and DateDiff("s", [YourTimeField] ,#2014/04/04 14:24:05#) >= 0

 

查询四:(OK)

select * from [YourTable] where
format([YourTimeField], "yyyymmddhhmmss") >= format(#2014/04/04 14:24:3#,"yyyymmddhhmmss")
and format([YourTimeField], "yyyymmddhhmmss") <= format(#2014/04/04 14:24:05#,"yyyymmddhhmmss")

 

posted @ 2014-04-14 15:21  sweetjian  阅读(1254)  评论(0编辑  收藏  举报