我需要使用 SQL 语言在 MS ACCESS 2016 中进行一些查询的帮助。我有一个包含 20 个雇主的表,该表包括姓名、姓氏、性别、出生日期、就业日期和付款。我需要计算具体年龄,我的意思是,我想知道我的雇主是谁,例如 25 岁,我不知道如何用 SQL 语言编写命令来计算,如果有人可以帮助我,我将不胜感激。
问问题
80 次
1 回答
0
您无法以简单的方式直接在 SQL 中计算 100% 正确的年龄。
所以使用这样的UDF(用户定义函数):
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
将代码复制并粘贴到一个空模块中。
然后运行查询:
Select *, AgeSimple([DateOfBirth]) As Age
From YourTable
Where AgeSimple([DateOfBirth]) >= 25
当然,用您的名称替换表和字段名称。
于 2016-11-07T15:48:48.730 回答