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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculating Age

Status
Not open for further replies.

rockman20

IS-IT--Management
Feb 2, 2004
34
0
0
US
CR 8.5 pulling from SQL2000 data through ODBC connection.

Okay, I have searched this site and I did get the formulas for calculating age and at first I thought that my problems were solved. However, I started looking through my results and I noticed that my age is not calculating correctly. I have tried this formula and can not get it to work.

DateTimeVar Birth:= {ASTU4004.BIRTHDATE}
DateTimeVar Ann := CurrentDate
if (Month(Ann) * 100) + Day (Ann) >=(Month(Birth) *100) + Day (Birth)
then Year (Ann) - Year(Birth)
else Year (Ann) - Year(Birth) -1


I have done this formula, but the age is not calculating correctly.

numbervar var_totalmonths := DateDiff ("m",{ASTU4004.BIRTHDATE},CurrentDate);
stringvar var_years := ToText(var_totalmonths/12);
stringvar var_yearstrimmed := If Length (var_years) = 5 Then (Left(var_years,2)) Else (Left(var_years,1));

Cstr(var_yearstrimmed) + "yrs "

Our birthday field has that dang annoying time in it as well. So birthdays look like 04/28/1978 12:00AM Is the time what is screwing this up?

What ends up happening is that everyone's age looks good except for the ones with birthdays this month. If they have a birthday in April, the formula already advanced their age even though they have not had their birthday yet.

I need this to be accurate since we are doing a census report for the government. Any help would be greatly appreciated.
 
The first formula is the formula you want to use, so please tell us what is not working correctly.

~Brian
 
Nothing. That is the problem. Absolutely nothing shows up in my report. CR claims there is nothing wrong with the formula, but my report remains blank.

If I put the second formula in my report right next to the first formula, I get results there, but nothing on the first one.
 
Not sure if it's a typo or not but you are missing semicolons at the end of you declarations

Code:
DateTimeVar Birth:= {ASTU4004.BIRTHDATE}[b][COLOR=red];[/color][/b]
DateTimeVar Ann := CurrentDate[b][COLOR=red];[/color][/b]
if (Month(Ann) * 100) + Day (Ann) >=(Month(Birth) *100) + Day (Birth)  
then Year (Ann) - Year(Birth)  
else Year (Ann) - Year(Birth) -1

-LW
 
Shoot, actually, it is a typo in both, my post and in the report. I am not sure why CR did not complain about this when I did the formula, but I just added in those two simple semi colons and all is well.

Thank you for pointing out the easy fix! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top