Tips and Tricks
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Tips and Tricks
Message Icon Topic: solution to DateDiff rounding error Post Reply Post New Topic
Author Message
Cordellv
Newbie
Newbie
Avatar

Joined: 14 Jan 2009
Location: United States
Online Status: Offline
Posts: 35
Quote Cordellv Replybullet Topic: solution to DateDiff rounding error
    Posted: 18 Nov 2009 at 8:44am
We have always used this formula to know how old a person is on a given day: 
 

truncate(DateDiff("d",{?prmBirthdate},{?Effective Date})/365,0)

 
However we recently discoverd that there is a rounding error and if the report is run the day before a person's birthday after about age 3 it starts to be off by .0027 and then finally by one full day (an then there is Feb 29th in leap years).
 
So here are two formulas that we found that work without that error:
 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

datediff('yyyy',{?prmBirthdate},{?Effective Date}) - (if datepart('y',{?Effective Date})>=datepart('y',{?prmBirthdate}) then 0 else 1)

 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

NumberVar CurrentYrBirthdateVar := IIF(100 * month({?Effective Date}) + day({?Effective Date}) < 100 *

month({?prmBirthdate}) + day({?prmBirthdate}),1,0);

 

NumberVar AgeAsofEffectiveDate := DATEDIFF("yyyy",

{?prmBirthdate},{?Effective Date}) - CurrentYrBirthdateVar;

 

AgeAsofEffectiveDate;

 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Hope that helps someone some whereClapClapClap
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Cordell
 
Making Things Better One Day At A time
IP IP Logged
Post Reply Post New Topic
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum



This page was generated in 0.032 seconds.