Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2020 : Technical Questions
Message Icon Topic: formula to calculate age Post Reply Post New Topic
Author Message
sanchezgmc06
Senior Member
Senior Member
Avatar

Joined: 21 Jan 2011
Online Status: Offline
Posts: 107
Quote sanchezgmc06 Replybullet Topic: formula to calculate age
    Posted: 27 Apr 2012 at 12:01pm
Hi i need a formula to calculate the clients age. I have the date of birth field
 
thanks
IP IP Logged
yggdrasil
Senior Member
Senior Member
Avatar

Joined: 19 Jun 2007
Location: United Kingdom
Online Status: Offline
Posts: 150
Quote yggdrasil Replybullet Posted: 30 Apr 2012 at 3:31am
numbervar dys;
numbervar yrs;
days :=DateDiff ('d', date_of_birth,currentdate );
yrs := int(dys/365);
dys := dys - yrs * 365;
Age will be  'yrs' years  and 'dys' days. 
 
If you want age as decimal years just use yrs := dys/365
 
 
IP IP Logged
DBlank
Moderator
Moderator


Joined: 19 Dec 2008
Online Status: Offline
Posts: 9053
Quote DBlank Replybullet Posted: 30 Apr 2012 at 3:59am
another approach is to use the datediff and datepart together to avoid leap year issues...
 
datediff('yyyy',{table.DOB},currentdate)-(if datepart('y',currentdate)>datepart('y',{table.DOB}) then 0 else 1)


Edited by DBlank - 30 Apr 2012 at 3:59am
IP IP Logged
aaml216
Newbie
Newbie
Avatar

Joined: 02 Nov 2011
Online Status: Offline
Posts: 24
Quote aaml216 Replybullet Posted: 30 Apr 2012 at 6:37am
DBlank - this is so helpful - I copied and pasted it right into my formula editor (changing table.field).  How do I get age in months? (babies)
Amanda from the Mid-Atlantic
IP IP Logged
DBlank
Moderator
Moderator


Joined: 19 Dec 2008
Online Status: Offline
Posts: 9053
Quote DBlank Replybullet Posted: 30 Apr 2012 at 6:51am
how are you defining months exactly?
IP IP Logged
kostya1122
Senior Member
Senior Member
Avatar

Joined: 13 Jun 2011
Online Status: Offline
Posts: 475
Quote kostya1122 Replybullet Posted: 30 Apr 2012 at 6:59am
this could work if the kids are under 1
(currentdate - {MEMB_COMPANY_V.BIRTH})/365*12
IP IP Logged
aaml216
Newbie
Newbie
Avatar

Joined: 02 Nov 2011
Online Status: Offline
Posts: 24
Quote aaml216 Replybullet Posted: 30 Apr 2012 at 7:02am
kostya - I'll give that a try.  It looks like how I write formulas in Excel. 
 
DBlank - I am only looking at kids under 2 so I want to see:
--3 months old rather than 0 years old
--19 months old rather than 1 year old
Amanda from the Mid-Atlantic
IP IP Logged
DBlank
Moderator
Moderator


Joined: 19 Dec 2008
Online Status: Offline
Posts: 9053
Quote DBlank Replybullet Posted: 30 Apr 2012 at 7:09am
this does not look to see the days of the month but it would be sometning like this
 
if
//months
(datediff('yyyy',{@dob},currentdate)-(if datepart('y',currentdate)>datepart('y',{@dob}) then 0 else 1))<2 then
totext(datediff('m',{@dob},currentdate),0,'') & ' months'
else
//years
totext((datediff('yyyy',{@dob},currentdate)-(if datepart('y',currentdate)>datepart('y',{@dob}) then 0 else 1)),0,'') & ' years'
IP IP Logged
sanchezgmc06
Senior Member
Senior Member
Avatar

Joined: 21 Jan 2011
Online Status: Offline
Posts: 107
Quote sanchezgmc06 Replybullet Posted: 30 Apr 2012 at 8:09am
perfect! thanks
IP IP Logged
dbodell
Groupie
Groupie
Avatar

Joined: 08 Nov 2011
Location: United States
Online Status: Offline
Posts: 53
Quote dbodell Replybullet Posted: 30 Apr 2012 at 9:59am
It's actually more accurate if you divide by 365.25 due to leap years.
Thanks,
D. Bodell
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.062 seconds.