Click here to Skip to main content
15,881,600 members
Articles / Programming Languages / SQL
Alternative
Tip/Trick

Get an Accurate Age by Date

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
19 Feb 2010CPOL 7.8K  
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)


Written By
Architect Pengdows
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --