Calculating Age in Years, Months and Days in SQL Server 2012

In this article, I would like to show the difference between two dates in years, months and days in SQL Server. In this article we take an existing date and the current date and using the "GetDate" function to find the years, months and days. The "DATEDIFF" and "Datepart" functions are used to determine years, months and days between two dates. So let's have a look at a practical example of how to calculate age in SQL Server 2012. The example is developed in SQL Server 2012 using the SQL Server Management Studio. 

First of all we will see the use of the "DATEDIFF" and "Datepart" functions.

The DATEDIFF Function

The SQL Server "DATEDIFF" Function is used to calculate the difference between two dates.  

Syntax

The syntax of the "DATEDIFF" built-in date function is as follows:

DATEDIFF(Datepart, Startdate, Enddate)

Here, The "Datepart" parameter is the part of the datetime to calculate the difference of. The Datepart can be any of the following:

Ms         Milliseconds
Yy          Year
Qq         Quarter of the Year
Mm        Month
Dy          The Day of the Year
Dd         Day of the Month
Wk         Week
Dw         The Day of the Week
Hh          Hour
Mi          Minute
Ss           Second

Example

Declare @dateofbirth datetime

Declare @currentdatetime datetime

Declare @years varchar(4)

set @dateofbirth = '1986-03-15' --Birthdate

set @currentdatetime  = getdate() --Current Datetime

select @years = datediff(year,@dateofbirth,@currentdatetime)

select @years   + ' years,' as years

Output

DATEDIFF-Function-in-SQL-Server.jpg

The Datepart Function

The SQL Server "Datepart" function returns a portion of a SQL Server Datetime field. 

Syntax

The syntax of the "Datepart" built-in date function is as follows :

DATEPART ([Date part], [Datetime])

Here, the <Date part> parameter is the part of the datetime. Datetime is the name of a SQL Server Datetime field and portion is one of the following:

Ms        Milliseconds
Yy         Year
Qq        Quarter of the Year
Mm      Month
Dy        The Day of the Year
Dd        Day of the Month
Wk       Week
Dw       The Day of the Week
Hh       Hour
Mi        Minute
Ss         Second
Example 

Declare@dateofbirthdatetime

Declare@currentdatetimedatetime

Declare@daysvarchar(3)

set@dateofbirth='1986-03-15'--Birthdate

set@currentdatetime =getdate()--Current Datetime

select@days=datepart(d,@currentdatetime)-datepart(d,@dateofbirth)

select @days   +' days' asDays

 

Output

DatePart-Function-in-SQL-Server.jpg

Calculating Age in years, months and days

Here is an example to get the years, months and days between two dates.

Declare@dateofbirthdatetime

Declare@currentdatetimedatetime

Declare@yearsvarchar(40)

Declare@monthsvarchar(30)

Declare@daysvarchar(30)

set@dateofbirth='1986-03-15'--birthdate

set@currentdatetime =getdate()--current datetime

select@years=datediff(year,@dateofbirth,@currentdatetime)-- To find Years

select@months=datediff(month,@dateofbirth,@currentdatetime)-(datediff(year,@dateofbirth,@currentdatetime)*12)

-- To Find Months

select@days=datepart(d,@currentdatetime)-datepart(d,@dateofbirth)-- To Find Days

select@years  +' years,   ' +@months +' months,   '+@days   +' days' asYearMonthDay 

Output

 

calculating-age-in-SQL-Server.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all