JAB
JAB

Reputation: 3616

String comparisons in JasperReports expressions

A database field named income_source is queried using:

SELECT * FROM table_name WHERE income_source LIKE "salaried%"

This retrieves income_source values with a "salaried" prefix. In iReport, the PrintWhenExpression value for the field is set as:

$F{income_source}.equals("Salaried")? Boolean.TRUE:Boolean.FALSE  

Why does the report output differ from the SQL output?

Upvotes: 4

Views: 21581

Answers (1)

Dave Jarvis
Dave Jarvis

Reputation: 31161

There are a few problems:

  • The value "salaried%" in the SQL differs from the value of "Salaried" in the expression.
  • The value "salaried%" uses the % to match all text after the letter d.
  • There is a bit of redundancy in the PrintWhenExpression.

Try the following expression:

$F{income_source}.startsWith( "salaried" )

Or:

$F{income_source}.trim().toLowerCase().startsWith( "salaried" )

One of those should work. You will also want to ensure Blank when null is checked. Otherwise, the expression becomes:

$F{income_source} == null ? Boolean.FALSE :
  $F{income_source}.trim().toLowerCase().startsWith( "salaried" )

Upvotes: 7

Related Questions