## Working Out Exact Age with DateDiff

 Author Message TSQL Tryer SSCarpal Tunnel Group: General Forum Members Points: 4497 Visits: 718 Hi There, I am using the following in my SELECT Statement - DATEDIFF (year,dbo.[CORE_CO-PERSON].[D-O-B],GETDATE())AS Age,It is pulling me back the age - but it is not exact.For example I have a person born on 01-08-1977, however his age is being calculated as 32 when it should be 31. Is there anyway I can get the age to be more exact?Many ThanksRyan Michael Valentine Jones SSC-Dedicated Group: General Forum Members Points: 35744 Visits: 11933 The logic for Age in this select statment shows a simple way to do it.`declare @date_of_birth datetimeset @date_of_birth = '19770801'select AGE = -- Find difference in years and subtract 1 if date is before this years birthday datediff(yy,@date_of_birth, a.date) - case when dateadd(yy,datediff(yy, @date_of_birth, a.DATE),@date_of_birth) > a.DATE then 1 else 0 end, DATE_OF_BIRTH = @date_of_birth, a.DATEfrom -- Test data from Date Table Function F_TABLE_DATE -- Function code available here: -- http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=61519 F_TABLE_DATE('20090725','20090805') aorder by a.DATE`Results:`AGE DATE_OF_BIRTH DATE---- ----------------------- -----------------------31 1977-08-01 00:00:00.000 2009-07-25 00:00:00.00031 1977-08-01 00:00:00.000 2009-07-26 00:00:00.00031 1977-08-01 00:00:00.000 2009-07-27 00:00:00.00031 1977-08-01 00:00:00.000 2009-07-28 00:00:00.00031 1977-08-01 00:00:00.000 2009-07-29 00:00:00.00031 1977-08-01 00:00:00.000 2009-07-30 00:00:00.00031 1977-08-01 00:00:00.000 2009-07-31 00:00:00.00032 1977-08-01 00:00:00.000 2009-08-01 00:00:00.00032 1977-08-01 00:00:00.000 2009-08-02 00:00:00.00032 1977-08-01 00:00:00.000 2009-08-03 00:00:00.00032 1977-08-01 00:00:00.000 2009-08-04 00:00:00.00032 1977-08-01 00:00:00.000 2009-08-05 00:00:00.000(12 row(s) affected)` Steve Jones SSC Guru Group: Administrators Points: 328231 Visits: 20105 The reason is that you're subtracting years, and as Michael's logic shows, you need to account for where in the current year you are in relation to the date of birth. Follow me on Twitter: @way0utwestForum Etiquette: How to post data/code on a forum to get the best helpMy Blog: www.voiceofthedba.com TSQL Tryer SSCarpal Tunnel Group: General Forum Members Points: 4497 Visits: 718 If below is my currect Select, where would I input your soloution please?SELECT dbo.[CORE_CO-PERSON].[PERSON-REF]AS Person_Reference, dbo.[CORE_CO-PERSON].[PERSON-TITLE] AS Person_Title, dbo.[CORE_CO-PERSON].[INITIALS]AS Initials, dbo.[CORE_CO-PERSON].[FORENAMES] AS Forenames, dbo.[CORE_CO-PERSON].[SURNAME]AS Surname, dbo.[IH_RE-TENANCY].[corr-name1]AS Name_On_Tenancy1, dbo.[IH_RE-TENANCY].[corr-name2]AS Name_On_Tenancy2, dbo.[CORE_CO-PERSON].[D-O-B] AS DateOfBirth, DATEDIFF (year,dbo.[CORE_CO-PERSON].[D-O-B],GETDATE())AS Age, dbo.[CORE_CO-PERSON].[NHI-NO] AS NationalInsurranceNumber, dbo.[CORE_CO-PERSON].[ORIGIN-CODE]AS EthnicOrigin, dbo.[CORE_CO-PERSON].[GENDER]AS Gender, dbo.[IH_RE-TENANCY].[tenancy-ref]AS Tenancy_Referance, dbo.[IH_RE-TENANCY].[tncy-start]AS Tenancy_StartDate, dbo.[IH_RE-TNCY-PERSON].[ON-TNCY] AS On_The_TenancyFROM dbo.[IH_RE-TENANCY] INNER JOIN dbo.[IH_RE-TNCY-PERSON] ON dbo.[IH_RE-TENANCY].[TNCY-SYS-REF] = dbo.[IH_RE-TNCY-PERSON].[TNCY-SYS-REF] INNER JOIN dbo.[CORE_CO-PERSON] ON dbo.[IH_RE-TNCY-PERSON].[PERSON-REF] = dbo.[CORE_CO-PERSON].[PERSON-REF]WHERE dbo.[IH_RE-TENANCY].[tncy-status] = 'CUR' AND dbo.[IH_RE-TENANCY].[tenancy-ref] NOT LIKE 'FS%' AND dbo.[IH_RE-TNCY-PERSON].[END-DATE]IS NULL --Need to only include NULL records for National Insurrance. --Only need to pull back those that are 16 and over. Dave Ballantyne SSCoach Group: General Forum Members Points: 16924 Visits: 8370 Also datediff counts boundaries, not calendarstry this`select datediff(yy,'31dec2009','01jan2010')` Clear Sky SQLMy Blog Mark Cowne SSChampion Group: General Forum Members Points: 14679 Visits: 26568 Michael Valentine Jones (7/20/2009)The logic for Age in this select statment shows a simple way to do it.`declare @date_of_birth datetimeset @date_of_birth = '19770801'select AGE = -- Find difference in years and subtract 1 if date is before this years birthday datediff(yy,@date_of_birth, a.date) - case when dateadd(yy,datediff(yy, @date_of_birth, a.DATE),@date_of_birth) > a.DATE then 1 else 0 end, DATE_OF_BIRTH = @date_of_birth, a.DATEfrom -- Test data from Date Table Function F_TABLE_DATE -- Function code available here: -- http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=61519 F_TABLE_DATE('20090725','20090805') aorder by a.DATE`Results:`AGE DATE_OF_BIRTH DATE---- ----------------------- -----------------------31 1977-08-01 00:00:00.000 2009-07-25 00:00:00.00031 1977-08-01 00:00:00.000 2009-07-26 00:00:00.00031 1977-08-01 00:00:00.000 2009-07-27 00:00:00.00031 1977-08-01 00:00:00.000 2009-07-28 00:00:00.00031 1977-08-01 00:00:00.000 2009-07-29 00:00:00.00031 1977-08-01 00:00:00.000 2009-07-30 00:00:00.00031 1977-08-01 00:00:00.000 2009-07-31 00:00:00.00032 1977-08-01 00:00:00.000 2009-08-01 00:00:00.00032 1977-08-01 00:00:00.000 2009-08-02 00:00:00.00032 1977-08-01 00:00:00.000 2009-08-03 00:00:00.00032 1977-08-01 00:00:00.000 2009-08-04 00:00:00.00032 1977-08-01 00:00:00.000 2009-08-05 00:00:00.000(12 row(s) affected)`You can also use datepart`AGE = -- Find difference in years and subtract 1 if date is before this years birthday datediff(yy,@date_of_birth, a.date) - case when datepart(dy, @date_of_birth) > datepart(dy, a.DATE) then 1 else 0 end,`or even this`AGE = -- Find difference in years and subtract 1 if date is before this years birthday datediff(yy,@date_of_birth, a.date) - (sign(datepart(dy, @date_of_birth)-datepart(dy, a.DATE))+1)/2,` ____________________________________________________Deja View - The strange feeling that somewhere, sometime you've optimised this query beforeHow to get the best help on a forumhttp://www.sqlservercentral.com/articles/Best+Practices/61537 TSQL Tryer SSCarpal Tunnel Group: General Forum Members Points: 4497 Visits: 718 AGE = -- Find difference in years and subtract 1 if date is before this years birthday datediff(yy,@date_of_birth, a.date) - case when datepart(dy, @date_of_birth) > datepart(dy, a.DATE) then 1 else 0 end,Thank you for all that have replied. I still have no idea how to incorporate the above code into my current select statement (see below)SELECT dbo.[CORE_CO-PERSON].[PERSON-REF]AS Person_Reference,dbo.[CORE_CO-PERSON].[PERSON-TITLE] AS Person_Title,dbo.[CORE_CO-PERSON].[INITIALS]AS Initials,dbo.[CORE_CO-PERSON].[FORENAMES] AS Forenames,dbo.[CORE_CO-PERSON].[SURNAME]AS Surname,dbo.[IH_RE-TENANCY].[corr-name1]AS Name_On_Tenancy1,dbo.[IH_RE-TENANCY].[corr-name2]AS Name_On_Tenancy2,dbo.[CORE_CO-PERSON].[D-O-B] AS DateOfBirth,DATEDIFF (year,dbo.[CORE_CO-PERSON].[D-O-B],GETDATE())AS Age,dbo.[CORE_CO-PERSON].[NHI-NO] AS NationalInsurranceNumber,dbo.[CORE_CO-PERSON].[ORIGIN-CODE]AS EthnicOrigin,dbo.[CORE_CO-PERSON].[GENDER]AS Gender,dbo.[IH_RE-TENANCY].[tenancy-ref]AS Tenancy_Referance,dbo.[IH_RE-TENANCY].[tncy-start]AS Tenancy_StartDate,dbo.[IH_RE-TNCY-PERSON].[ON-TNCY] AS On_The_TenancyFROM dbo.[IH_RE-TENANCY]INNER JOIN dbo.[IH_RE-TNCY-PERSON]ON dbo.[IH_RE-TENANCY].[TNCY-SYS-REF] = dbo.[IH_RE-TNCY-PERSON].[TNCY-SYS-REF]INNER JOIN dbo.[CORE_CO-PERSON]ON dbo.[IH_RE-TNCY-PERSON].[PERSON-REF] = dbo.[CORE_CO-PERSON].[PERSON-REF]WHERE dbo.[IH_RE-TENANCY].[tncy-status] = 'CUR'AND dbo.[IH_RE-TENANCY].[tenancy-ref] NOT LIKE 'FS%'AND dbo.[IH_RE-TNCY-PERSON].[END-DATE]IS NULL--Need to only include NULL records for National Insurrance.--Only need to pull back those that are 16 and over. I'm afraid I am a total novice when it comes to this, so sorry. Mark Cowne SSChampion Group: General Forum Members Points: 14679 Visits: 26568 Ryan Keast (7/20/2009)AGE = -- Find difference in years and subtract 1 if date is before this years birthday datediff(yy,@date_of_birth, a.date) - case when datepart(dy, @date_of_birth) > datepart(dy, a.DATE) then 1 else 0 end,Thank you for all that have replied. I still have no idea how to incorporate the above code into my current select statement (see below)SELECT dbo.[CORE_CO-PERSON].[PERSON-REF]AS Person_Reference,dbo.[CORE_CO-PERSON].[PERSON-TITLE] AS Person_Title,dbo.[CORE_CO-PERSON].[INITIALS]AS Initials,dbo.[CORE_CO-PERSON].[FORENAMES] AS Forenames,dbo.[CORE_CO-PERSON].[SURNAME]AS Surname,dbo.[IH_RE-TENANCY].[corr-name1]AS Name_On_Tenancy1,dbo.[IH_RE-TENANCY].[corr-name2]AS Name_On_Tenancy2,dbo.[CORE_CO-PERSON].[D-O-B] AS DateOfBirth,DATEDIFF (year,dbo.[CORE_CO-PERSON].[D-O-B],GETDATE())AS Age,dbo.[CORE_CO-PERSON].[NHI-NO] AS NationalInsurranceNumber,dbo.[CORE_CO-PERSON].[ORIGIN-CODE]AS EthnicOrigin,dbo.[CORE_CO-PERSON].[GENDER]AS Gender,dbo.[IH_RE-TENANCY].[tenancy-ref]AS Tenancy_Referance,dbo.[IH_RE-TENANCY].[tncy-start]AS Tenancy_StartDate,dbo.[IH_RE-TNCY-PERSON].[ON-TNCY] AS On_The_TenancyFROM dbo.[IH_RE-TENANCY]INNER JOIN dbo.[IH_RE-TNCY-PERSON]ON dbo.[IH_RE-TENANCY].[TNCY-SYS-REF] = dbo.[IH_RE-TNCY-PERSON].[TNCY-SYS-REF]INNER JOIN dbo.[CORE_CO-PERSON]ON dbo.[IH_RE-TNCY-PERSON].[PERSON-REF] = dbo.[CORE_CO-PERSON].[PERSON-REF]WHERE dbo.[IH_RE-TENANCY].[tncy-status] = 'CUR'AND dbo.[IH_RE-TENANCY].[tenancy-ref] NOT LIKE 'FS%'AND dbo.[IH_RE-TNCY-PERSON].[END-DATE]IS NULL--Need to only include NULL records for National Insurrance.--Only need to pull back those that are 16 and over. I'm afraid I am a total novice when it comes to this, so sorry.Add this to the WHERE clause`AND DATEDIFF (year,dbo.[CORE_CO-PERSON].[D-O-B],GETDATE()) -CASE WHEN DATEPART(dy,dbo.[CORE_CO-PERSON].[D-O-B]) > DATEPART(dy, GETDATE()) THEN 1 ELSE 0 END >= 16` ____________________________________________________Deja View - The strange feeling that somewhere, sometime you've optimised this query beforeHow to get the best help on a forumhttp://www.sqlservercentral.com/articles/Best+Practices/61537 Lynn Pettis SSC Guru Group: General Forum Members Points: 223136 Visits: 40398 I also need to ask a question, what about leaplings? If some one is born on February 29th, when do you need to consider them a year older, on February 28th or March 1st? Based on this will actually determine how you calculate age. Lynn PettisFor better assistance in answering your questions, click hereFor tips to get better help with Performance Problems, click hereFor Running Totals and its variations, click here or when working with partitioned tablesFor more about Tally Tables, click hereFor more about Cross Tabs and Pivots, click here and hereManaging Transaction LogsSQL Musings from the Desert Fountain Valley SQL (My Mirror Blog) Lynn Pettis SSC Guru Group: General Forum Members Points: 223136 Visits: 40398 Or`WHERE ... and [CORE_CO-PERSON].[D-O-B] <= datediff(yy, -16, getdate()) -- where DOB is less than or equal to todays date 16 years ago.`Edit:After 3 years, someone caught that I had the right idea but the wrong function in the code above. This is what I should have posted here:`WHERE ... and [CORE_CO-PERSON].[D-O-B] <= dateadd(yy, -16, getdate()) -- where DOB is less than or equal to todays date 16 years ago.` Lynn PettisFor better assistance in answering your questions, click hereFor tips to get better help with Performance Problems, click hereFor Running Totals and its variations, click here or when working with partitioned tablesFor more about Tally Tables, click hereFor more about Cross Tabs and Pivots, click here and hereManaging Transaction LogsSQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)