Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Age calculation in DB2

Status
Not open for further replies.

ari1

Programmer
Jun 17, 2002
7
FI
In Orcale there is a function MONTHS_BETWEEN and in SQL Server DateDiff which help to calculate age from a birthdate. Is there a similar function in DB2 to get a difference of dates (in months)?

 
I don't know of a similar function in DB2. I use this code when I want to get differences in date:

Select ((year(date_1) - year(date_2)) * 12) +
month(date_1) - month(date_2)


You could try posting this question in the DB2 forum to see if anyone there knows of a function I haven't heard of.
 
Thank you, but I can't see how this works? Multiply years by 12? The problem is, that difference between
1990-06-19 and 2002-06-19 => 12 years, and the differnece 1990-06-20 and 2002-06-19 => 11 years.
 
Ifound a working solution with help from Sathyaram (dBforums):
select current date, date_1 , integer( floor( (current date - date_1 ) / 10000 )) as age from table_1.
This works now: difference between
1990-06-19 and 2002-06-19 => 12 years, and the difference 1990-06-20 and 2002-06-19 => 11 years.

Why i have to divide with 10000 is still unclear...
 
Just a thought, you may be able to simply substract the higher date from the lower one.
Your result should be the number of days between.
 
Dear angiole
In DB2 the difference comes in format yymmdd, that's why it must first be divided by 10000 to get eg 12,0512 and then "floored" to 12,0 and finally "integered" to 12, meaning the age of 12.
Ari
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top