Calculating a Person's Age in Crystal Reports

One common problem encountered by those utilizing MS SQL when trying to calculate a person's age is that DateDiff will return the age that the person will be that year. In otherwords assume you run the query on March 1, 2002 and you have one individual born on January 2, 1973 and another on March 25, 1973. In this case DateDiff will return 29 for both people. To get around this you must use a conditional statement, this example shows the Case statement.

The code would be:

If (Not(isNull({PERSON.DOB}))) Then
     If (Month({PERSON.DOB})>Month(CurrentDate()) or
       (Month({PERSON.DOB})=Month(CurrentDate()) and
       Day({PERSON.DOB})>Day(CurrentDate()))) Then
          DateDiff('yyyy',{PERSON.DOB},CurrentDate())-1
     Else
          DateDiff('yyyy',{PERSON.DOB},CurrentDate())

First step is to check for Null - Crystal deals very poorly with Nulls and its best if you do a check for it. Then check to see if the person's birth day has occured, if not then use the DateDiff function minus one otherwise just use the DateDiff function.

@Copyright 2002-2004 Keptin Consulting Services. All Rights Reserved