Date Subtraction in iReports

I’m using iReports a lot for general business reporting, which is an open source reporting tool in the spirit of Crystal Reports.  It merges SQL and Java, creating report “documents” which are an XML file which tells how to attach to the database, run the query, and then format the results into a pretty document with fonts and things.

A lot of times you’re working with dates and trying to understand the age of things.  iReports lets you create variables and attach Java code to them, which gives infinite flexibility for logic and conversion.  Unless, of course, you don’t know Java in which case its a bewildering concoction of spaces, periods, parens, and references to mysterious methods and objects in classes buried in attached, unseen, places.  A visit to a number of forums focused on Java or iReports will connect you to Java experts who berate you to read API documentation.

Or, use the DATEFIFF function in SQL (MySQL, in this case) to do the math and assign it to a SQL field:

SELECT
 DATEDIFF(table.`date_pickedup`, table.`date_arrival`) AS agedInSystem,
 DATEDIFF(table.`date_pickedup`, table.`date_service`) AS agedSinceDOS
FROM table

No point spending time in Java when SQL has a canned function for this.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.