Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / SQL

Get an Accurate Age by Date

5.00/5 (2 votes)
19 Feb 2010CPOL 7.9K  
This would work too :)As an explanation, I use datediff to get the number of days, divide that by the correct number of days in a year (keeping track of leap years, there are only 97 in every 400 years) and then FLOOR the result, thus making sure it doesn't get rounded up accidentally....
This would work too :)

As an explanation, I use datediff to get the number of days, divide that by the correct number of days in a year (keeping track of leap years, there are only 97 in every 400 years) and then FLOOR the result, thus making sure it doesn't get rounded up accidentally.

SQL
CREATE FUNCTION [dbo].[GetAge]
    (
      @Geburtsdatum DATETIME
    )
RETURNS INT
AS 
    BEGIN
        RETURN   FLOOR(DATEDIFF(dd, @Geburtsdatum, CURRENT_TIMESTAMP)/365.2425)
    END
GO

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)