2016-11-07 76 views
0

我需要使用SQL語言的MS ACCESS 2016中的某些查詢幫助。 我有一張有20位僱主的表格,表格中包括姓名,性別,出生日期,就業和付款日期。 我需要計算具體年齡,我的意思是,我想知道誰是我的僱主,例如25歲,我不知道如何編寫SQL語言的命令來計算,如果有人可以幫助我,我將非常感激。MS ACCESS 2016中的SQL語言

+2

向我們展示您已經取得的成就! – duDE

+0

添加樣品表數據和預期結果。 (以及格式化文本) – jarlh

+0

好吧,如果你想寫一個查詢,那麼開始的地方就是學習語言。所有教程,視頻,書籍,文檔,示例,課程和在線教程都可以幫助人們學習SQL和其他計算機語言。 –

回答

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 

當然,與你的替換表和字段名稱。