MS Access 在两个日期之间进行选择?
Posted
技术标签:
【中文标题】MS Access 在两个日期之间进行选择?【英文标题】:MS Access select between two dates? 【发布时间】:2016-02-11 04:51:40 【问题描述】:我已经搜索过,但所有结果都没有帮助我理解。
我需要选择 18-23 岁的人的姓名。 所以我的尝试是:
WHERE ((People.Birth) Between (Now()-Year(18)) And (Now()-Year(23)))
我做错了什么? #some_date# 的解决方案是个坏主意!
【问题讨论】:
您调查过Year(18)
实际返回的内容吗?
见datediff。
f*ck,理解了很多次,所有问题都需要在睡觉后解决,而不是在一天 25 小时后解决。 Year(18) 返回 1900... 2 user2864740 - 我见过,但对我没有帮助(
【参考方案1】:
对于真正的解决方案,您需要使用 DateAdd 和这样的函数:
Public Function AgeSimple( _
ByVal datDateOfBirth As Date) _
As Integer
' Returns the difference in full years from datDateOfBirth to current date.
'
' Calculates correctly for:
' leap years
' dates of 29. February
' date/time values with embedded time values
'
' DateAdd() is used for check for month end of February as it correctly
' returns Feb. 28. when adding a count of years to dates of Feb. 29.
' when the resulting year is a common year.
' After an idea of Markus G. Fischer.
'
' 2007-06-26. Cactus Data ApS, CPH.
Dim datToday As Date
Dim intAge As Integer
Dim intYears As Integer
datToday = Date
' Find difference in calendar years.
intYears = DateDiff("yyyy", datDateOfBirth, datToday)
If intYears > 0 Then
' Decrease by 1 if current date is earlier than birthday of current year
' using DateDiff to ignore a time portion of datDateOfBirth.
intAge = intYears - Abs(DateDiff("d", datToday, DateAdd("yyyy", intYears, datDateOfBirth)) > 0)
End If
AgeSimple = intAge
End Function
那么你的查询将有这个 where 子句:
WHERE AgeSimple(People.Birth) Between 18 And 23
【讨论】:
【参考方案2】:终于通过简单的方式解决了
WHERE ((People.Birth) Between (Now()- 365*18) And (Now()-365*23))
如果你有更好的解决方案-欢迎
【讨论】:
以上是关于MS Access 在两个日期之间进行选择?的主要内容,如果未能解决你的问题,请参考以下文章