MS ACCESS 2016中的SQL语言
问题描述:
我需要使用SQL语言的MS ACCESS 2016中的某些查询帮助。 我有一张有20位雇主的表格,表格中包括姓名,性别,出生日期,就业和付款日期。 我需要计算具体年龄,我的意思是,我想知道谁是我的雇主,例如25岁,我不知道如何编写SQL语言的命令来计算,如果有人可以帮助我,我将非常感激。MS ACCESS 2016中的SQL语言
答
无法用简单的方法直接在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
当然,与你的替换表和字段名称。
向我们展示您已经取得的成就! – duDE
添加样品表数据和预期结果。 (以及格式化文本) – jarlh
好吧,如果你想写一个查询,那么开始的地方就是学习语言。所有教程,视频,书籍,文档,示例,课程和在线教程都可以帮助人们学习SQL和其他计算机语言。 –