What would be the best way to calculate someone's age in years, months, and days in T-SQL (SQL Server 2000)?
The datediff
function doesn't handle year boundaries well, plus getting the months and days separate will be a bear. I know I can do it on the client side relatively easily, but I'd like to have it done in my stored procedure.
Here is some T-SQL that gives you the number of years, months, and days since the day specified in @date. It takes into account the fact that DATEDIFF() computes the difference without considering what month or day it is (so the month diff between 8/31 and 9/1 is 1 month) and handles that with a case statement that decrements the result where appropriate.
I use this Function I modified (the Days part) From @Dane answer: https://stackoverflow.com/a/57720/2097023
It works pretty well.
Simple way to get age as text is as below:
Results Format will be:
Quite Old question, but I want to share what I have done to calculate age
Implemented by arithmetic with ISO formatted date.
Test Cases
The approach of days is different from the accepted answer, the differences shown in the comments below:
An short version of Days by case statement:
If you want the age of years and months only, it could be simpler
NOTE: A very useful link of SQL Server Date Formats