Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
查询特定时间长度(Sql访问)_Sql_Database_Ms Access - Fatal编程技术网

查询特定时间长度(Sql访问)

查询特定时间长度(Sql访问),sql,database,ms-access,Sql,Database,Ms Access,所以我知道如何查询特定的日期范围,但不知道具体的时间长度 因此,我不是在寻找某个人从x日期到y日期受雇的记录,而是在寻找他们受雇5年的记录。有点怪,但是有人对此有什么见解吗?使用DATEDIFF函数 WHERE DATEDIFF ( yy, startdate , enddate ) = 5 DateDiff只返回日历年的差异,因此您需要一个自定义函数来使用DateAdd,如下所示: Public Function AgeSimple( _ ByVal datDateOfBirth As

所以我知道如何查询特定的日期范围,但不知道具体的时间长度


因此,我不是在寻找某个人从x日期到y日期受雇的记录,而是在寻找他们受雇5年的记录。有点怪,但是有人对此有什么见解吗?

使用
DATEDIFF
函数

WHERE DATEDIFF ( yy, startdate , enddate ) = 5 

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
猴子先生:

请注意完整解释的在线评论。例如:

' 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

示例数据和所需结果将有助于解释您想要执行的操作。您不能只在开始日期上添加X秒/毫秒吗?大多数日期都存储为一个数字,表示一个小的时间单位(秒和毫秒是最常用的单位)。你知道它们是什么时候开始的,如果它们仍然在工作,你知道今天是什么,如果它们没有工作,你知道它们的结束日期。所以你计算出这两者之间经过的时间,然后计算出它是5年还是更长。这几乎是一个单一的,你可以在几年内得到你的结果,以避免你自己做任何计算来解释闰年之类的。这只会返回日历年的差异。这看起来像是一个非常复杂的日期差异,我真的希望你是在开玩笑。然而,我已经在答案中添加了一个扩展的解释,这应该可以清楚地说明目的。请研究并将您的反对票更改为赞成票。感谢您提供的额外信息和示例输出,我知道您使用此代码实现了什么,我的投票已更改为+1