所以我知道如何查询特定的日期范围,但不知道特定的时间长度。
因此,我不是在寻找从x日期到约会期间雇用某人的记录,而是在寻找他们已被雇用5年的记录。有点时髦,但有没有人对此有任何见解?
答案 0 :(得分:3)
DateDiff只返回日历年的差异,因此您需要一个使用 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
然后使用类似于的SQL:
Select *
From PersonTable
Where AgeSimple([EmploymentDate]) = 5
对于Monkey先生:
请注意完整解释的在线评论。例如:
' Decrease by 1 if current date is earlier than birthday of current year
为了实现这一目的,我们添加了三条“非常复杂”的代码行:
If intYears > 0 Then
intAge = intYears - Abs(DateDiff("d", datToday, DateAdd("yyyy", intYears, datDateOfBirth)) > 0)
End If
为了说明这些的必要性,请考虑以下两个例子:
EmploymentDate = #2001/5/1#
Employed = DateDiff("yyyy", EmploymentDate, Date) ' -> 15 years
Employed = AgeSimple(EmploymentDate) ' -> 15 years
EmploymentDate = #2002/11/15#
Employed = DateDiff("yyyy", EmploymentDate, Date) ' -> 14 years
Employed = AgeSimple(EmploymentDate) ' -> 13 years
答案 1 :(得分:-1)
使用WHERE DATEDIFF ( yy, startdate , enddate ) = 5
功能
swipl -f testing.pl -g test(X),halt