Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
551 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Written by Andrew Gould
In this tutorial
A common requirement in SQL Server databases is to calculate the age of something in years. There are several techniques for doing this depending on how accurate you want the final result to be. This tutorial considers three techniques, saving the most complex, but most accurate, for last.
Apparently, the quickest and easiest way to calculate the age of someone or something in years is to simply use the DATEDIFF function.
A seemingly quick and obvious way to calculate age in years.
At first glance the DATEDIFF function seems to successfully calculate ages quickly and easily, but closer inspection reveals that it's not quite as accurate as we'd like (and certainly not as accurate as Keanu Reeves would like!). In fact, in the above list, only the last record is calculated correctly - the other three are being reported as one year older than they actually are.
The problem is that when the DATEDIFF function is told to look at years it ignores every other part of the dates involved, essentially performing the calculation shown below:
This is what DATEDIFF is really doing when you ask it to give you the difference between two dates in years.
Clearly, using DATEDIFF to calculate the difference between dates in years isn't accurate enough for something as potentially sensitive as age. In that case we need a different approach.
A more accurate, but not perfect, way to calculate age in years is to first work out the difference in days between two dates and then divide the result by the number of days in a year. The method is shown in the example below:
Dividing the age in days by the number of days in a year gives a slightly more accurate result. The .25 is to take into account leap years.
The last step in this type of calculation is to remove the decimal places to give the age in whole years. To do this we can convert the answer to the INT data type.
Converting the result of the above calculation to the INT data type gives us the age in years.
Clearly this method is more accurate than using DATEDIFF with years, but we're still not one hundred percent there.
Here the date range doesn't include a leap year so we're inaccurately reporting the age as a year younger than it should be.
The problem with this method is that smaller date ranges will give us a less accurate answer, as in the example shown above. Fortunately, there's one further method that we can use to calculate age in years correctly.
This is the most accurate way to calculate an age in years, but it's also the most complex expression to write. The starting point is to use the first calculation we demonstrated at the top of the page to calculate the difference in years between two dates:
The original DATEDIFF calculation reports some dates as a year older than they actually are.
The next step is to incorporate the DATEADD function into the expression to add the calculated number of years to the original date:
This calculation tells us the date on which the event reaches the age shown in the third column.
The result of the above calculation is the date on which the person or thing reaches the age that the DATEDIFF function calculates. The final step is to work out whether that date is after today's date, and if so subtract 1 from the age that DATEDIFF calculates. We can use the CASE statement to do this as follows:
Finally, an accurately calculated age!
The expression is quite difficult to read, but it is the most accurate way to calculate an age in years in SQL Server. Phew!
You can learn more about this topic on the following Wise Owl courses:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.