Get an Accurate Age by Date





5.00/5 (2 votes)
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.
CREATE FUNCTION [dbo].[GetAge]
(
@Geburtsdatum DATETIME
)
RETURNS INT
AS
BEGIN
RETURN FLOOR(DATEDIFF(dd, @Geburtsdatum, CURRENT_TIMESTAMP)/365.2425)
END
GO