简体   繁体   English

MS ACCESS 2016中的SQL语言

[英]SQL language in MS ACCESS 2016

i need help with some query in MS ACCESS 2016 with SQL language. 我需要使用SQL语言在MS ACCESS 2016中进行某些查询的帮助。 I have a table with 20 employers, and the table includes name, last name, gender, date of birth, date of employment and payment. 我有一个包含20位雇主的表格,该表格包括姓名,姓氏,性别,出生日期,就业日期和付款方式。 I need to calculate specific age, I mean, I want to know who is for example 25 years old of my employers, i dont know how to write command in SQL language to calculate that, i would be very grateful if someone can help me. 我需要计算具体年龄,我的意思是,我想知道谁是我的雇主,例如25岁,我不知道如何用SQL语言编写命令来计算该年龄,如果有人可以帮助我,我将不胜感激。

You cannot calculate 100% correct age directly in SQL in a simple way. 您无法通过简单的方法直接在SQL中直接计算100%正确的年龄。

So use an UDF (user defined function) like this: 因此,使用这样的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

Copy and paste the code into an empty module. 将代码复制并粘贴到空模块中。

Then run a query: 然后运行查询:

Select *, AgeSimple([DateOfBirth]) As Age
From YourTable
Where AgeSimple([DateOfBirth]) >= 25

Of course, replace table and field names with those of yours. 当然,将表名和字段名替换为您的名字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM