In a SQL Server database, I record people\'s date of birth. Is there an straight-forward method of working out the person\'s age on a given date using SQL only?
Usi
FWIW, Age can be computed in a straightforward manner without resorting to hacks (not that there's anything wrong with hacks!):
CREATE FUNCTION Age (@BirthDate DATETIME)
RETURNS INT
AS
BEGIN
DECLARE @AgeOnBirthdayThisYear INT
DECLARE @BirthdayThisYear DATETIME
SET @AgeOnBirthdayThisYear = DATEDIFF(year, @BirthDate, GETDATE())
SET @BirthdayThisYear = DATEADD(year, @AgeOnBirthdayThisYear, @BirthDate)
RETURN
@AgeOnBirthdayThisYear
- CASE WHEN @BirthdayThisYear > GETDATE() THEN 1 ELSE 0 END
END