﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / T-SQL (SS2K8)  / Best way to Calculate Human Age from a Birthdate / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Fri, 24 May 2013 22:45:51 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Best way to Calculate Human Age from a Birthdate</title><link>http://www.sqlservercentral.com/Forums/Topic1404342-392-1.aspx</link><description>[quote][b]mdsharif532 (1/10/2013)[/b][hr]How about this.............. DECLARE @BIRTH_DATE DATETIME, @STPR_START_DATE DATETIMESET @BIRTH_DATE = '2008-02-29'SET @STPR_START_DATE = '2013-02-28'SELECT CASE WHEN YEAR(@STPR_START_DATE)%400 != 0 AND DAY(@STPR_START_DATE)= 28 THENDATEDIFF(YEAR, @BIRTH_DATE, @STPR_START_DATE) - CASE WHEN MONTH(@BIRTH_DATE)*100 + (DAY(@BIRTH_DATE) -1) &amp;gt; MONTH(@STPR_START_DATE)*100 + DAY(@STPR_START_DATE) THEN 1 ELSE 0 END ELSEDATEDIFF(YEAR, @BIRTH_DATE, @STPR_START_DATE) - CASE WHEN MONTH(@BIRTH_DATE)*100 + DAY(@BIRTH_DATE) &amp;gt; MONTH(@STPR_START_DATE)*100 + DAY(@STPR_START_DATE) THEN 1 ELSE 0 END END AS AGE[/quote]A lot of extra work for what can be done easily with a couple of datetime functions and a case statement.</description><pubDate>Thu, 10 Jan 2013 19:04:12 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>RE: Best way to Calculate Human Age from a Birthdate</title><link>http://www.sqlservercentral.com/Forums/Topic1404342-392-1.aspx</link><description>[quote][b]Michael Valentine Jones (1/9/2013)[/b][hr][quote][b]Jeff Moden (1/9/2013)[/b][hr]So, which "standard" are you going to use for someone that is born on a leap year day?  Feb 28th or Mar 1st? ;-)[/quote]As usual, no one agrees on this, but different countries do at least have some standard.[url]http://en.wikipedia.org/wiki/February_29[/url]"...in England and Wales or in Hong Kong, a person born on February 29, 1996, will have legally reached 18 years old on March 1, 2014. If he or she was born in the United States, Taiwan or New Zealand, he or she legally becomes 18 on February 28, 2014, a day earlier..."I prefer the Feb 28 date for birthdays in non-leap years because that's the "standard" for the US.  And, it's easier to code in TSQL.:-D[/quote]I hate to say it, but there is not actually a "standard" the U.S. - it depends on the precise purpose you're doing the date calculations for, and who needs them.  Check with the business users about this, every time, particularly in regulated industries for for regulated/legal purposes.</description><pubDate>Thu, 10 Jan 2013 11:47:51 GMT</pubDate><dc:creator>Nadrek</dc:creator></item><item><title>RE: Best way to Calculate Human Age from a Birthdate</title><link>http://www.sqlservercentral.com/Forums/Topic1404342-392-1.aspx</link><description>How about this.............. DECLARE @BIRTH_DATE DATETIME, @STPR_START_DATE DATETIMESET @BIRTH_DATE = '2008-02-29'SET @STPR_START_DATE = '2013-02-28'SELECT CASE WHEN YEAR(@STPR_START_DATE)%400 != 0 AND DAY(@STPR_START_DATE)= 28 THENDATEDIFF(YEAR, @BIRTH_DATE, @STPR_START_DATE) - CASE WHEN MONTH(@BIRTH_DATE)*100 + (DAY(@BIRTH_DATE) -1) &amp;gt; MONTH(@STPR_START_DATE)*100 + DAY(@STPR_START_DATE) THEN 1 ELSE 0 END ELSEDATEDIFF(YEAR, @BIRTH_DATE, @STPR_START_DATE) - CASE WHEN MONTH(@BIRTH_DATE)*100 + DAY(@BIRTH_DATE) &amp;gt; MONTH(@STPR_START_DATE)*100 + DAY(@STPR_START_DATE) THEN 1 ELSE 0 END END AS AGE</description><pubDate>Thu, 10 Jan 2013 08:25:07 GMT</pubDate><dc:creator>mdsharif532</dc:creator></item><item><title>RE: Best way to Calculate Human Age from a Birthdate</title><link>http://www.sqlservercentral.com/Forums/Topic1404342-392-1.aspx</link><description>How about these solutions?[code="sql"]declare @DOB date = '20080229',        @CurDate date = '20130228';select    @DOB as DateOfBirth,    @CurDate as CurrentDate,    datediff(yy, @DOB, @CurDate) - case when dateadd(yy, -datediff(yy, @DOB, @CurDate), @CurDate) &amp;lt; @DOB                                        then 1                                        else 0                                   end as TurnsYearOlderFirstOfMarch,    datediff(yy, @DOB, @CurDate) - case when dateadd(yy, datediff(yy, @DOB, @CurDate), @DOB) &amp;lt;= @CurDate                                        then 0                                        else 1                                   end as TurnsYearOlderLastOfFebruary;set @CurDate = '20130301';select    @DOB as DateOfBirth,    @CurDate as CurrentDate,    datediff(yy, @DOB, @CurDate) - case when dateadd(yy, -datediff(yy, @DOB, @CurDate), @CurDate) &amp;lt; @DOB                                        then 1                                        else 0                                   end as TurnsYearOlderFirstOfMarch,    datediff(yy, @DOB, @CurDate) - case when dateadd(yy, datediff(yy, @DOB, @CurDate), @DOB) &amp;lt;= @CurDate                                        then 0                                        else 1                                   end as TurnsYearOlderLastOfFebruary;set @CurDate = '20130227';select    @DOB as DateOfBirth,    @CurDate as CurrentDate,    datediff(yy, @DOB, @CurDate) - case when dateadd(yy, -datediff(yy, @DOB, @CurDate), @CurDate) &amp;lt; @DOB                                        then 1                                        else 0                                   end as TurnsYearOlderFirstOfMarch,    datediff(yy, @DOB, @CurDate) - case when dateadd(yy, datediff(yy, @DOB, @CurDate), @DOB) &amp;lt;= @CurDate                                        then 0                                        else 1                                   end as TurnsYearOlderLastOfFebruary;[/code]</description><pubDate>Thu, 10 Jan 2013 00:28:17 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>RE: Best way to Calculate Human Age from a Birthdate</title><link>http://www.sqlservercentral.com/Forums/Topic1404342-392-1.aspx</link><description>Simple way to compute your age accurately.A lot of query not exactly compute the age? yes! because sometime they only compute the datediff between DOB and datenow and divide it to 365.25 days and as a result they get a number with decimal something like 25 is the age with .06 decimal (age=25.06).In this query you exactly get the age as is it.Example 1DOB = 11/15/1987 and datenow =11/15/2012 the result would be AGE=25 Example 2DOB = 11/16/1987 and datenow =11/15/2012 the result would be AGE=24SO HERE ARE THE QUERYDECLARE @DOB SMALLDATETIMESELECT @DOB = '11/15/1987'SELECT CASE WHEN MONTH(@DOB) &amp;gt;= MONTH(GETDATE()) AND DAY(@DOB) &amp;gt;=DAY(GETDATE()) THEN DATEDIFF(YY,@DOB,GETDATE()) ELSE DATEDIFF(YY,@DOB,GETDATE())-1END AS AGEHOPE I CAN HELP!</description><pubDate>Wed, 09 Jan 2013 23:40:43 GMT</pubDate><dc:creator>math martinez</dc:creator></item><item><title>RE: Best way to Calculate Human Age from a Birthdate</title><link>http://www.sqlservercentral.com/Forums/Topic1404342-392-1.aspx</link><description>[quote][b]Jeff Moden (1/9/2013)[/b][hr][quote][b]AndrewSQLDBA (1/8/2013)[/b][hr]Hello EveryoneHappy New Year!!!I am fooling around with some code, and was wondering if there is a really great function to calculate the birthdate of a person. One that will take into count things like leap year. I am not getting into such fine details such as if the person was born on the west coast or the east coast type of calculations. Just a very good birthdate age calculation function.If I pass in the birthdate, what is the persons age today. Most that I have used or tried, come up a bit short when it comes to returning a perfect calculationThanks in AdvanceAndrew SQLDBA[/quote]So, which "standard" are you going to use for someone that is born on a leap year day?  Feb 28th or Mar 1st? ;-)[/quote]As usual, no one agrees on this, but different countries do at least have some standard.[url]http://en.wikipedia.org/wiki/February_29[/url]"...in England and Wales or in Hong Kong, a person born on February 29, 1996, will have legally reached 18 years old on March 1, 2014. If he or she was born in the United States, Taiwan or New Zealand, he or she legally becomes 18 on February 28, 2014, a day earlier..."I prefer the Feb 28 date for birthdays in non-leap years because that's the "standard" for the US.  And, it's easier to code in TSQL.:-D </description><pubDate>Wed, 09 Jan 2013 09:05:52 GMT</pubDate><dc:creator>Michael Valentine Jones</dc:creator></item><item><title>RE: Best way to Calculate Human Age from a Birthdate</title><link>http://www.sqlservercentral.com/Forums/Topic1404342-392-1.aspx</link><description>[quote][b]AndrewSQLDBA (1/8/2013)[/b][hr]Hello EveryoneHappy New Year!!!I am fooling around with some code, and was wondering if there is a really great function to calculate the birthdate of a person. One that will take into count things like leap year. I am not getting into such fine details such as if the person was born on the west coast or the east coast type of calculations. Just a very good birthdate age calculation function.If I pass in the birthdate, what is the persons age today. Most that I have used or tried, come up a bit short when it comes to returning a perfect calculationThanks in AdvanceAndrew SQLDBA[/quote]So, which "standard" are you going to use for someone that is born on a leap year day?  Feb 28th or Mar 1st? ;-)</description><pubDate>Wed, 09 Jan 2013 08:03:47 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Best way to Calculate Human Age from a Birthdate</title><link>http://www.sqlservercentral.com/Forums/Topic1404342-392-1.aspx</link><description>[quote]Going by the dictionary definition of age, "The length of time during which a being or thing has existed.", I have to say that negative age does not make much sense to me.[/quote]I agree that as age negative doesn't make any sense. I was trying to point out the calculation [i]could[/i] be used in other scenarios where a negative would make sense. Given that the thread is to calculate human age it certainly doesn't make any sense in that context.</description><pubDate>Wed, 09 Jan 2013 07:22:40 GMT</pubDate><dc:creator>Sean Lange</dc:creator></item><item><title>RE: Best way to Calculate Human Age from a Birthdate</title><link>http://www.sqlservercentral.com/Forums/Topic1404342-392-1.aspx</link><description>[quote][b]Sean Lange (1/8/2013)[/b][hr]Actually I think that a negative age has some real value if you think of age not as only the age of a person. What you are calculating is the total years elapsed between two dates and if you were looking at something like project delivery dates a negative data might have some real worth. Maybe a company that works with long term deliverables like construction. It might be nice to see that something was delivered more than a year ahead of schedule. Your fine code would work for such a scenario if it included negatives. We have now seen several different approaches to the same problem. Most of them handle leap years correctly too. :-D[/quote]Going by the dictionary definition of age, "The length of time during which a being or thing has existed.", I have to say that negative age does not make much sense to me.I tested the solutions from the other posts using the test data I posted (leaving out the negative ages), and found every one had at least one difference with the solution I posted, especially with the handling of Feb 29 birthdays, or when the time of day for CURR_DATE was before the time of day for DOB when they were both the same day of the year ( Example: DOB = 2013-01-08 16:52:54.810 and CURR_DATE = 2023-01-08 16:52:54.710 ).  I believe most calculations of Age ignore time of day, so my solution is coded to ignore it.</description><pubDate>Tue, 08 Jan 2013 15:34:43 GMT</pubDate><dc:creator>Michael Valentine Jones</dc:creator></item><item><title>RE: Best way to Calculate Human Age from a Birthdate</title><link>http://www.sqlservercentral.com/Forums/Topic1404342-392-1.aspx</link><description>Actually I think that a negative age has some real value if you think of age not as only the age of a person. What you are calculating is the total years elapsed between two dates and if you were looking at something like project delivery dates a negative data might have some real worth. Maybe a company that works with long term deliverables like construction. It might be nice to see that something was delivered more than a year ahead of schedule. Your fine code would work for such a scenario if it included negatives. We have now seen several different approaches to the same problem. Most of them handle leap years correctly too. :-D</description><pubDate>Tue, 08 Jan 2013 15:07:08 GMT</pubDate><dc:creator>Sean Lange</dc:creator></item><item><title>RE: Best way to Calculate Human Age from a Birthdate</title><link>http://www.sqlservercentral.com/Forums/Topic1404342-392-1.aspx</link><description>The key to finding age is to find the birthday for the current year, and subtract 1 from the difference in years if the current date is before the birthday this year.Note that the code below computes the current year birthday for Feb 29 birthdays as Feb 28 for non-leap years and Feb 29 for leap years.The concept of negative age doesn't seem to have any real world meaning, so my method returns null if the date of birth is before current date.[code="sql"]select 	a.DOB,	a.CURR_DATE,	Age =	datediff(yy,a.DOB,a.CURR_DATE) +	case	-- Age is null when DOB before current date	when datediff(dd,a.DOB,a.CURR_DATE) &amp;lt; 0	then null	-- Subtract 1 if current date before birthday in current year	when datediff(dd,dateadd(yy,datediff(yy,a.DOB,a.CURR_DATE),a.DOB),a.CURR_DATE) &amp;lt; 0	then -1 else 0 endfrom	( -- Test Data	select		DOB =convert(datetime,'20040407'),		CURR_DATE = convert(datetime,'20060203')	union all	select getdate(),dateadd(ms,100,getdate())  union all	select getdate(),dateadd(ms,-100,getdate())  union all	select getdate(),dateadd(dd,-1,getdate())  union all	select getdate(),dateadd(yy,10,dateadd(ms,-100,getdate()))  union all	select getdate(),dateadd(yy,10,dateadd(ms,100,getdate()))  union all	select '20040407','20040407'  union all	select '20040407','20050406'  union all	select '20040407','20050407'  union all	select '20040229','20060227'  union all	select '20040229','20060228'  union all	select '20040229','20060301'  union all	select '20040229','20080228'  union all	select '20040229','20080229'  union all	select '20060205','20060205'  union all	select '17530101 00:00:00.000','99991231 23:59:59.997'  union all	select '19500913', getdate()	) aorder by	a.DOB,	a.CURR_DATE[/code]Results:[code="plain"]DOB                     CURR_DATE                       Age----------------------- ----------------------- -----------1753-01-01 00:00:00.000 9999-12-31 23:59:59.997        82461950-09-13 00:00:00.000 2013-01-08 16:52:54.810          622004-02-29 00:00:00.000 2006-02-27 00:00:00.000           12004-02-29 00:00:00.000 2006-02-28 00:00:00.000           22004-02-29 00:00:00.000 2006-03-01 00:00:00.000           22004-02-29 00:00:00.000 2008-02-28 00:00:00.000           32004-02-29 00:00:00.000 2008-02-29 00:00:00.000           42004-04-07 00:00:00.000 2004-04-07 00:00:00.000           02004-04-07 00:00:00.000 2005-04-06 00:00:00.000           02004-04-07 00:00:00.000 2005-04-07 00:00:00.000           12004-04-07 00:00:00.000 2006-02-03 00:00:00.000           12006-02-05 00:00:00.000 2006-02-05 00:00:00.000           02013-01-08 16:52:54.810 2013-01-07 16:52:54.810        NULL2013-01-08 16:52:54.810 2013-01-08 16:52:54.710           02013-01-08 16:52:54.810 2013-01-08 16:52:54.910           02013-01-08 16:52:54.810 2023-01-08 16:52:54.710          102013-01-08 16:52:54.810 2023-01-08 16:52:54.910          10[/code]</description><pubDate>Tue, 08 Jan 2013 14:54:07 GMT</pubDate><dc:creator>Michael Valentine Jones</dc:creator></item><item><title>RE: Best way to Calculate Human Age from a Birthdate</title><link>http://www.sqlservercentral.com/Forums/Topic1404342-392-1.aspx</link><description>[quote][b]mdsharif532 (1/8/2013)[/b][hr]DECLARE @BIRTH_DATE DATETIME, @STPR_START_DATE DATETIMESET @BIRTH_DATE = '1975-01-07'SET @STPR_START_DATE = '2013-01-06'SELECT DATEDIFF(YEAR, @BIRTH_DATE, @STPR_START_DATE)Result: 38SELECT DATEDIFF(YEAR, @BIRTH_DATE, @STPR_START_DATE) - CASE WHEN MONTH(@BIRTH_DATE)*100 + DAY(@BIRTH_DATE) &amp;gt; MONTH(@STPR_START_DATE)*100 + DAY(@STPR_START_DATE) THEN 1 ELSE 0 END AS AGEResult: 37[/quote]OK now it makes sense. I just couldn't figure out what the dates were supposed to be. :-D</description><pubDate>Tue, 08 Jan 2013 14:29:22 GMT</pubDate><dc:creator>Sean Lange</dc:creator></item><item><title>RE: Best way to Calculate Human Age from a Birthdate</title><link>http://www.sqlservercentral.com/Forums/Topic1404342-392-1.aspx</link><description>DECLARE @BIRTH_DATE DATETIME, @STPR_START_DATE DATETIMESET @BIRTH_DATE = '1975-01-07'SET @STPR_START_DATE = '2013-01-06'SELECT DATEDIFF(YEAR, @BIRTH_DATE, @STPR_START_DATE)Result: 38SELECT DATEDIFF(YEAR, @BIRTH_DATE, @STPR_START_DATE) - CASE WHEN MONTH(@BIRTH_DATE)*100 + DAY(@BIRTH_DATE) &amp;gt; MONTH(@STPR_START_DATE)*100 + DAY(@STPR_START_DATE) THEN 1 ELSE 0 END AS AGEResult: 37</description><pubDate>Tue, 08 Jan 2013 13:23:41 GMT</pubDate><dc:creator>mdsharif532</dc:creator></item><item><title>RE: Best way to Calculate Human Age from a Birthdate</title><link>http://www.sqlservercentral.com/Forums/Topic1404342-392-1.aspx</link><description>What is STPRD.STPR_START_DATE? Student Program Start Date</description><pubDate>Tue, 08 Jan 2013 13:16:33 GMT</pubDate><dc:creator>mdsharif532</dc:creator></item><item><title>RE: Best way to Calculate Human Age from a Birthdate</title><link>http://www.sqlservercentral.com/Forums/Topic1404342-392-1.aspx</link><description>What about this: [url]http://www.sqlservercentral.com/articles/T-SQL/63351/[/url].</description><pubDate>Tue, 08 Jan 2013 12:16:54 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>RE: Best way to Calculate Human Age from a Birthdate</title><link>http://www.sqlservercentral.com/Forums/Topic1404342-392-1.aspx</link><description>[quote][b]mdsharif532 (1/8/2013)[/b][hr]DATEDIFF(YEAR,P.BIRTH_DATE,STPRD.STPR_START_DATE) - CASE WHEN MONTH(P.BIRTH_DATE)*100 + DAY(P.BIRTH_DATE) &amp;gt; MONTH(STPRD.STPR_START_DATE)*100 + DAY(STPRD.STPR_START_DATE) THEN 1 ELSE 0 END AS AGE,[/quote]You really should look at the link I posted. It is simple, accurate and easy to understand which the above is not. This looks like you took this from a query you have in your system somewhere. What is STPRD.STPR_START_DATE?</description><pubDate>Tue, 08 Jan 2013 12:13:31 GMT</pubDate><dc:creator>Sean Lange</dc:creator></item><item><title>RE: Best way to Calculate Human Age from a Birthdate</title><link>http://www.sqlservercentral.com/Forums/Topic1404342-392-1.aspx</link><description>DATEDIFF(YEAR,P.BIRTH_DATE,STPRD.STPR_START_DATE) - CASE WHEN MONTH(P.BIRTH_DATE)*100 + DAY(P.BIRTH_DATE) &amp;gt; MONTH(STPRD.STPR_START_DATE)*100 + DAY(STPRD.STPR_START_DATE) THEN 1 ELSE 0 END AS AGE,</description><pubDate>Tue, 08 Jan 2013 11:27:09 GMT</pubDate><dc:creator>mdsharif532</dc:creator></item><item><title>RE: Best way to Calculate Human Age from a Birthdate</title><link>http://www.sqlservercentral.com/Forums/Topic1404342-392-1.aspx</link><description>Thank You SeanThat worked perfectly.Greatly appreciate itAndrew SQLDBA</description><pubDate>Tue, 08 Jan 2013 11:18:53 GMT</pubDate><dc:creator>AndrewSQLDBA</dc:creator></item><item><title>RE: Best way to Calculate Human Age from a Birthdate</title><link>http://www.sqlservercentral.com/Forums/Topic1404342-392-1.aspx</link><description>There are tons of articles and forum posts around here. This is about the best in my opinion.[url=http://www.sqlservercentral.com/Forums/Topic796803-338-1.aspx]http://www.sqlservercentral.com/Forums/Topic796803-338-1.aspx[/url]</description><pubDate>Tue, 08 Jan 2013 09:42:38 GMT</pubDate><dc:creator>Sean Lange</dc:creator></item><item><title>Best way to Calculate Human Age from a Birthdate</title><link>http://www.sqlservercentral.com/Forums/Topic1404342-392-1.aspx</link><description>Hello EveryoneHappy New Year!!!I am fooling around with some code, and was wondering if there is a really great function to calculate the birthdate of a person. One that will take into count things like leap year. I am not getting into such fine details such as if the person was born on the west coast or the east coast type of calculations. Just a very good birthdate age calculation function.If I pass in the birthdate, what is the persons age today. Most that I have used or tried, come up a bit short when it comes to returning a perfect calculationThanks in AdvanceAndrew SQLDBA</description><pubDate>Tue, 08 Jan 2013 09:33:48 GMT</pubDate><dc:creator>AndrewSQLDBA</dc:creator></item></channel></rss>