quote: Originally posted by nosepicker This should also help you determine someone's age:DECLARE @birthdate datetimeSET @birthdate = CONVERT(datetime, '19670405')SELECT DATEDIFF(year, @birthdate, getdate()) - ( (DATEPART(dy, @birthdate) -1) / DATEPART(dy, getdate()) )
The two methods shown disagree on the way they handle birthdays on February 29.The F_AGE_YYYY_MM_DD function will make February 28 the birthday in non-leap years.The method posted by nosepicker will make March 1 the birthday in non-leap years.select [Age 1] = DATEDIFF(year, FR, TD) - ( (DATEPART(dy, FR) -1) / DATEPART(dy, TD) ) , [Age 2] = dbo.F_AGE_YYYY_MM_DD( FR, TD ), FR = convert(varchar(10),FR,121), TD = convert(varchar(10),TD,121)from ( select FR = convert(datetiMe,'19670404'), TD = getdate() union all select FR = convert(datetiMe,'19670405'), TD = getdate() union all select FR = convert(datetiMe,'19670406'), TD = getdate() union all select FR = convert(datetiMe,'20000229'), TD = '20030228' union all select FR = convert(datetiMe,'20000229'), TD = '20030301' union all select FR = convert(datetiMe,'20000229'), TD = '20040228' union all select FR = convert(datetiMe,'20000229'), TD = '20040229' ) a Results:Age 1 Age 2 FR TD ----------- ---------- ---------- ---------- 39 0039 00 01 1967-04-04 2006-04-0539 0039 00 00 1967-04-05 2006-04-0538 0038 11 30 1967-04-06 2006-04-052 0003 00 00 2000-02-29 2003-02-28 Disagreement 3 0003 00 01 2000-02-29 2003-03-013 0003 11 31 2000-02-29 2004-02-284 0004 00 00 2000-02-29 2004-02-29(7 row(s) affected) CODO ERGO SUM |