#### Introduction:

As part of the blog series TSQL - Solve it YOUR Way, today's topic will cover a question asked recently in the T-SQL MSDN forums where the solution, at first, seems extremely obvious.  However, as solutions were proposed and flaws were discovered with the proposals, the interesting nature of the problem was revealed.  As is the theme of the blog series, different solutions and explanations are provided from some of the most helpful and creative contributors in the TSQL MSDN forums.  This week, the contributors are Steven Wang, Olaf Helper, Tom Cooper, and Monica Rivera.

Topic:  Determine the age of a person given their birth date.

A fun aspect to this question is the fact that we've all probably made these calculations hundreds of times in our minds throughout our lives as we hear birth dates in conversation so often.  The result is that it seems that the calculation should be so simple.  However, since SQL Server does not have an AGE() function that takes a datetime parameter and returns an age, we'll need to handle the logic and calculate it ourselves.  You can then store the result as a function in your SQL Server DBA/DB Developer toolbox and have the solution forever.  Three of the below solutions are somewhat similar using some variation of DATEFIFF(), but vary slightly in interesting ways, while one (Steven Wang's solution) takes on a completely new and interesting approach.  This is an outstanding example of why I've enjoyed collaborating with other SQL Server professional on this series to highlight different methods and solutions to a single problem.  The solutions show the true power of SQL Server as a programming language and also highlight the freedom that we as DBA's, DB developers, BI experts, etc. have in creating our solutions.  I hope you enjoy the solutions as much as I have.

#### Solution #1 - Provided by Olaf Helper

Code Snippet
1. DECLARE @reportDate datetime;
2. SET @reportDate = CONVERT(date,getdate());
3.
4. ;WITH
5.   birthdates AS
6.    (SELECT 'Peter' AS Name, {d N'1970-09-14'} AS Birthdate UNION ALL
7.     SELECT 'Bob' AS Name, {d N'1970-11-27'} AS Birthdate UNION ALL
8.     SELECT 'Sabina' AS Name, {d N'1970-11-28'} AS Birthdate UNION ALL
9.     SELECT 'Amy' AS Name, {d N'1970-11-29'} AS Birthdate UNION ALL
10.     SELECT 'Jane' AS Name,  {d N'1970-12-31'} AS Birthdate UNION ALL
11.     SELECT 'Julie' AS Name, {d N'1971-12-31'} AS Birthdate)
12. ,age AS
13.    (SELECT *
14.           ,DATEDIFF(Year, Birthdate, @reportDate) AS Age
15.           ,DATEADD(Year, DATEDIFF(Year, Birthdate, @reportDate), BirthDate) AS SwitchDate
16.     FROM birthdates)
17.
18. SELECT age.Name
19.       ,age.Birthdate
20.       ,CASE WHEN SwitchDate <= @reportDate
21.             THEN age
22.             ELSE age - 1 END AS Age
23.       ,CASE WHEN SwitchDate = @reportDate
24.             THEN 'Happy Birthday!!!'
25.             ELSE '' END AS Gratulation
26. FROM age
27. ORDER BY age.Birthdate;

Explanation of Olaf's solution:

The CTE "birthdates" creates sample data around the current date for reporting, plus some additional data to check the result.

The DateDiff with option "YEAR" function seems the first and obvious way to calculate the age, but it returns only the difference of the year part and doesn't account for the additional months.
For example:

SELECT DATEDIFF(YEAR, {d N'2000-01-01'}, {d N'2001-01-01'})
,DATEDIFF(YEAR, {d N'2000-01-01'}, {d N'2001-12-31'})

This returns 1 for both results, even though in the second example, the difference is "nearly" 2 years.

To solve this I still use the DateDiff function, then add the difference between the birth and the given date to the birthdate. In a CASE WHEN statement I compare the result with the given date and if it's higher, I subtract 1 to get the real age.

#### Solution #2 - Provided by Tom Cooper

Code Snippet
1. Declare @Test Table(EmpName varchar(40), BirthDate date);
2. Insert @Test(EmpName, BirthDate) Values
4. ('30 Yrs old today', DateAdd(year, -30, GetDate())),
6.
7. With cte As
8. (Select EmpName, BirthDate, DateDiff(year, BirthDate, GetDate()) As ProvisionalAge
9. From @Test)
10. Select EmpName, BirthDate,
11.   ProvisionalAge - Case When DateAdd(year, ProvisionalAge, BirthDate) > GetDate() Then 1 Else 0 End As Age
12. From cte;

Explanation of Tom's solution:

This uses the fact that when you do a DATEDIFF() in years between birth date and the current date, the number you get is either the correct age or one year too large depending on whether or not the person has had a birthday in this year.  That can be determined by adding that number of years to the birth date and checking to see if you get a date greater than today.  If so, subtract 1.

#### Solution #3 - Provided by Monica Rivera

Code Snippet
2. GO
3.
5. CASE WHEN DATEADD(YY,DATEDIFF(yy,BirthDate,GETDATE()),BirthDate)<GETDATE() THEN DATEDIFF(yy,BirthDate,GETDATE())
6. ELSE DATEDIFF(yy,BirthDate,GETDATE())-1 END AS AGE
7. from HumanResources.Employee
8. order by BirthDate

Explanation of Monica's solution:

There are two cases when calculating a person’s age:

1.  The birth date has already happened in the current year: the age is simply the current year minus the year of birth.
2.  The birth date has not yet happened in the current year: the age is equal to the current year minus the year of birth minus one.

A simple T-SQL expression to determine if the birth date has already happened in the current year is the following:

This adds to the birth date the difference in years between to the birth date and today’s date. If this exceeds today’s date, then we are in case 2.

Plug it in to a case statement that describes the two cases above and you get the solution.

#### Solution #4 - Provided by Steven Wang

Code Snippet
1. --Extract some Test Data using the AdventureWorks sample database
2. If Object_ID('dbo.employee_birthdate') is not Null
3. Drop table dbo.employee_birthdate;
4.   SELECT[NationalIDNumber]
5.   , [BirthDate]
6. Into dbo.employee_birthdate
7. FROM [HumanResources].[Employee]
8. Union All
9. Select -111111, '2000-02-29'
10. Union All
11. Select -222222, '2004-02-29'
12. Union All
13. Select -333333, Cast(Getdate() as Date);
14. --Actual solution
15. SELECT [NationalIDNumber], [BirthDate]
16.   ,(Convert(Char(8), CURRENT_TIMESTAMP,112) - 0 - Convert(char(8), [BirthDate], 112)) / 10000 As Age
17. FROM [dbo].[employee_birthdate]
18. Order by Age;

Explanation of Steven's solution:

1. To calculate the age based on birth date needs to take account of 2 considerations: year difference and the date was born.
2. Use the year difference as the base of the age calculation. If the current date is less than the date was born, then the age needs to minus 1 year.
3. Inspired by the dimensional surrogate integer date key, we can use the date integer format to perform the calculation. For example, if a birthdate is ‘2001-09-30’, and current date is ‘2012-10-01’, then we can use the difference (20121001 – 20010930) / 10000 to get the age 11. As in T-SQL Integer / Integer is always Integer. In the same example, if the current date is ‘2012-09-29’, then the result (20120929 – 20010930) / 10000 will be 10.
4. As we can’t directly convert the date format to integer value, I convert the date to char(8) first and use minus 0 to implicitly convert the char value to integer value.

#### Conclusion:

As you can see, all four of the above solutions provide the result we were looking for, but do so in creatively different styles.  Each of these solutions highlights the power and flexibility that SQL Server provides. I hope that you are able to learn a lot by trying out the problem yourself and then reading through the additional solutions.

Special thanks to Olaf, Tom, Monica, and Steven for their valuable forums contribution and for contributing to this series!

Hope that helps,
Sam Lester (MSFT)

#### Contributor Bios:

Steven Wang has worked with SQL server for more than 10 years. Steven is an active SQL server community participant and speaks at events such as TechEd, CodeCamp, SQL User Group etc.

Blog: www.MSBICOE.com | LinkedIn: Steven Wang | MSDN Profile: Steven Wang - Shangzhou

Tom Cooper began his programming career in 1968, began working with database software in 1977, and first worked with Microsoft SQL Server in 1994 (version 4.21).  He is now very happily retired.

Monica Rivera has been working with SQL Server for the past 10 years. She worked as a DBA before joining Microsoft, where she works as a tester for the SQL Server Enterprise & Tier 1 Manageability group.  She is very active in both the English and Spanish SQL Server MSDN forums.