Use the 日期序列函数计算给定日期该月的最后一天。
传递零作为第三个参数,day,实际上返回最后一个日期previous month.
rdate = #2013-7-24#
? DateSerial(Year(rdate), Month(rdate), 0)
6/30/2013
因此要获取最后日期rdate
月,添加1
to the month争论。
? DateSerial(Year(rdate), Month(rdate) + 1, 0)
7/31/2013
您可能怀疑这种方法会在 12 月失效rdate
, since Month() + 1
将返回 13。但是,DateSerial
仍然应付它。
rdate = #2013-12-1#
? DateSerial(Year(rdate), Month(rdate) + 1, 0)
12/31/2013
如果要从 Access 应用程序会话中运行查询,则可以基于该方法构建 VBA 函数,并在查询中使用自定义函数。
但是,如果查询将从 ODBC 或 OleDb 连接到 Access 数据库运行,则查询不能使用 VBA 用户定义函数。在这种情况下,您可以使用DateSerial
直接在您的查询中。
SELECT m.*
FROM mytable AS m
WHERE m.rdate = DateSerial(Year(m.rdate), Month(m.rdate) + 1, 0)
如果你的rdate
值均包含午夜作为时间部分。如果这些值包括其他时间,请使用DateValue
.
WHERE DateValue(m.rdate) = DateSerial(Year(m.rdate), Month(m.rdate) + 1, 0)