Calculate Age using DateDiff function in SQL Server 2008
In this article I describe how to calculate age with the datediff function.
Introduction
In this article I describe how to calculate age with the datediff function. The DATEDIFF function is used to return the difference between two dates. It calculates the time between start and end time.
Syntax
The syntax for using datediff() built-in function is given below:
DATEDIFF (datepart,startdate,enddate)
|
where,
DatePart parameter is used to calculate the difference. The datepart can be one of the following:
DatePart |
Symbol |
Year |
yy |
Quarter of the year |
qq |
Day |
dd |
Day of the year |
dy |
Month |
mm |
Week |
wk |
Day of the week |
dw |
Second |
ss |
Miliseconds |
ms |
Hour |
hh |
Example
Calculate age wit the help of datediff function:
Select getdate() as CurrentDate
Go
Declare @CurrentDate datetime = getdate()
Declare @DOB datetime = '1986-03-15'
Select DATEDIFF( Year, @DOB, @CurrentDate) as CurrentAgeinYears
|
Output: