PDA

View Full Version : Age Calculation in SQL


aurobindo
07-19-02, 13:05
I was trying to calculate the age of a person in years, months and days as on present day. The input is the birth date while in yyyymmdd format. The age in yyyymmdd fromat is to be returned thru a select statement. Infact the date of births is already there in a table and you will have return the present age. Use of cursor is not prefered. Can anyone help me on this please. Aurobindo

rnealejr
07-23-02, 00:03
Which database are you using ?

jdlambert1
07-28-02, 14:55
You can use a SELECT query rather than a cursor by substracting the birthdate from the system date. How you do that depends on the brand you're using, because vendors use different functions for determining the current date and for extracting parts of dates. For example, to retrieve the system date, DB2 uses CURRENT DATE, Oracle uses SYSDATE, SQL Server and Sybase use GETDATE.

Let us know which database software you're using and we'll try to tell you how you can structure the query.

WingMan
08-19-02, 12:59
For SQL SEVER GETDATE() returns the current date

You can then use the DATEDIFF Function to calculate the difference :D

tamaldutta
08-27-02, 07:30
Originally posted by rnealejr
Which database are you using ?
for oracle, you may try this ......
i am sending it without testing , but i hope it will work with some modifications (if required at all!)

SELECT floor(months_between(sysdate,<column_name>)/12)||floor((months_between(sysdate,<column_name>) - floor(months_between(sysdate,<column_name>)/12)) from <table_name>

Marcel Crudele
08-06-07, 16:59
I just ran across this problem and was sure there would be a function out there to handle it, but couldn't find one. Just for the reusability of it, I wrote one for SQL 2005...

=======================================
How to calculate age in 2 easy steps…
=======================================
1. Run the following script in any SQL 2005 DB you want to determine Age in.
2. Calculate age anytime you want by sending a start date and end date in the format
select dbo.CalcAge(@StartDate, @EndDate)




The return value will be a decimal in the format of number of years old plus a decimal representing the number of days since the last anniversary date divided by the number of days until the next anniversary date – this does take into account leap years so be careful.



An example of how to break this information out as age in years and days since last anniversary is shown below (there is a precision issue, but ROUND seems to handle it). @NumDaysPerYear should be the number of days between last anniversary and next anniversary if you take into account leap year (365 or 366). The way to calculate @NumDaysPerYear is included in the function script.

select dbo.CalcAge(@StartDate, @EndDate) as AgeInYears, floor(dbo.CalcAge(@StartDate, @EndDate) ) AS AgeYear, cast(ROUND(((dbo.CalcAge(@StartDate, @EndDate) -floor(dbo.CalcAge(@StartDate, @EndDate) )) * @NumDaysPerYear),0) as smallint) As DaysSinceLastAnniversary



--===========================
-- RUN THIS SCRIPT TO
-- CREATE CalcAge Function
--============================

CREATE FUNCTION dbo.CalcAge (@StartDate datetime, @EndDate datetime)

RETURNS decimal(18,9)

WITH EXECUTE AS CALLER

AS

BEGIN

DECLARE @Age decimal(18,9), @RecentAnniversary smalldatetime, @UpcomingAnniversary smalldatetime,

@NumDaysPerYear int

--SELECT @StartDate='10/18/1910', @EndDate='8/11/2000'

-- DETERMINE THE MOST RECENT ANNIVERSARY DATE AND NEXT ANNIVERSARY DATE.

-- THIS WILL BE USED FOR DETERMINING THE DECIMAL PART OF AGE.

select @RecentAnniversary=cast(cast(month(@StartDate) as varchar(2)) + '/' + cast(day(@StartDate) as varchar(2)) + '/' + cast(year(@EndDate) as varchar(4)) as smalldatetime)

if @RecentAnniversary>@EndDate

BEGIN

select @UpcomingAnniversary=@RecentAnniversary

select @RecentAnniversary=cast(cast(month(@StartDate) as varchar(2)) + '/' + cast(day(@StartDate) as varchar(2)) + '/' + cast(year(@EndDate)-1 as varchar(4)) as smalldatetime)

END

else

select @UpcomingAnniversary=cast(cast(month(@StartDate) as varchar(2)) + '/' + cast(day(@StartDate) as varchar(2)) + '/' + cast(year(@EndDate)+1 as varchar(4)) as smalldatetime)

-- NOW DETERMINE THE NUMBER OF DAYS BETWEEN THE ANNIVERSARY DATES. T

-- THIS WILL TAKE INTO ACCOUNT LEAP YEARS

select @NumDaysPerYear = DateDiff(day, @RecentAnniversary, @UpcomingAnniversary)

-- GET THE BASE AGE OF OF THE DATES

SELECT @Age=DATEDIFF (YYYY, @StartDate, @EndDate) -

CASE

WHEN (MONTH(@StartDate)=MONTH(@EndDate) AND DAY(@StartDate) > DAY(@EndDate)

OR MONTH (@StartDate) > MONTH (@EndDate))

THEN 1

ELSE 0

END

-- NOW GET THE DECIMAL PART

--select @Age, DateDiff(day, @RecentAnniversary, @EndDate), @NumDaysPerYear

Select @Age=@Age + cast(DateDiff(day, @RecentAnniversary, @EndDate) as decimal(12,9)) / cast(@NumDaysPerYear as decimal(12,9))

-- TEST BREAKING OUT COMPONENTS. THERE ARE SOME PRECISION ISSUES, BUT THE ROUNDING

-- BELOW SEEMS TO ADDRESS IT

--select @Age as Age, floor(@Age) AS AgeYear, cast(ROUND(((@Age-floor(@Age)) * @NumDaysPerYear),0) as smallint) As DaysSinceLastAnniversary

RETURN (@Age)

END;

GO

-- ================
-- END SCRIPT
-- ================

--Marcel Crudele
--innerEcho, Inc.