Skip to main content
Inspiring
March 16, 2010
Question

Calculate age in a function

  • March 16, 2010
  • 1 reply
  • 4388 views

I am trying to calculate and return the age of a person. My code is:

    <cfset variable.age = '(dateDiff("yyyy",query.birthdate, now()))'>

I get a "If the expression is a string, ensure that it is within quotes. Error: report.ModStat(query.soc_sec)

is not a valid ColdFusion expression." error message when I try to run the report.

Any help would be appreciated.

Thanks.

This topic has been closed for replies.

1 reply

Inspiring
March 16, 2010

The error you are quoting is not related to the line of code you quote, for starters.  So I think you're troubleshooting this froom the wrong place in your code.

Secondly, this:

    <cfset variable.age = '(dateDiff("yyyy",query.birthdate, now()))'>

Simply sets variable.age to the string  '(dateDiff("yyyy",query.birthdate, now()))'.  IE: exactly that string.  You should not have the outer single quotes (and the outer parentheses are unnecessary too).

Should the statement be setting variable.age or variables.age?

The first thing you should be doing when confronted with code that is breaking is to be inspecting the values of the contributing variables.

--

Adam

Participating Frequently
March 16, 2010

In addition to the feedback above, many functions like that have been made and are posted at CFLib.org.

http://www.cflib.org/udf/AgeSinceDOB

Cozmo2Author
Inspiring
March 16, 2010

I checked the CFLib.org site and could not figure out how to change the script into a function in report builder.

I removed the outer single quotes & parentheses and changed my statement to variables.age.

The report works fine until I put the dateDiff code into the fucntion. I am able to return the mod_stat (from the query)

can't get the DateDiff code to work.

The function reads a table looking for specific mod_stat. If the mod_stat is not found and the calculated age

is 21 yrs old or greater I want to return an "Over 21" message to the report. If the mod_stat is found or the person

is under 21 yrs. I want to return a space. The birthdate comes from the query attached to the report.

Any suggestions?

Thanks