Need to create a basic Age Calculator function which calculates the age in years on the age field of the peoples table.
需要创建一个基本的年龄计算器函数,该函数计算人员表的年龄字段中的年龄。
The function should be called agecalculator, it needs to take 1 date and calculate the age in years according to the date NOW and must return an integer.
该函数应该称为agecalculator,它需要花费1个日期并根据NOW的日期计算年龄,并且必须返回一个整数。
You may query the people table while testing but the query must only contain the function on your final submit.
您可以在测试时查询人员表,但查询必须仅包含最终提交的功能。
people table schema
人员表架构
id
name
age
This is my code:
这是我的代码:
select convert(int,DATEDIFF(d, '1933-10-31', getdate())/365.25)
1 个解决方案
#1
0
Try this:
DECLARE @DOB DATETIME ='12/29/1980'
SELECT @DOB 'Date of Birth', GETDATE() 'Current Date',
DATEDIFF(YEAR,@DOB,GETDATE()) -
(CASE WHEN DATEADD(YY,DATEDIFF(YEAR,@DOB,GETDATE()),@DOB) > GETDATE()
THEN 1
ELSE 0
END) 'Age in Years'
#1
0
Try this:
DECLARE @DOB DATETIME ='12/29/1980'
SELECT @DOB 'Date of Birth', GETDATE() 'Current Date',
DATEDIFF(YEAR,@DOB,GETDATE()) -
(CASE WHEN DATEADD(YY,DATEDIFF(YEAR,@DOB,GETDATE()),@DOB) > GETDATE()
THEN 1
ELSE 0
END) 'Age in Years'