Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8515711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:13:24+00:00 2026-06-11T05:13:24+00:00

Let’s say I want a JasperReport that lets the user filter on a date

  • 0

Let’s say I want a JasperReport that lets the user filter on a date if they so wish. The SQL is as follows:

select * from foo where bar = $P{bar} and some_date > $P{some.date}

Now, I don’t want to filter by some date if they didn’t pass the date in. I found the following kludge that people use:

select * from foo where bar = $P{bar} $P!{some.date.fragment}

And the some.date.fragment parameter is defined with the following default:

($P{some.date} == null || $P{some.date}.equals("")) ? "" : "AND some_date >'" + new java.sql.Date($P{some.date}.getTime()).toString() + "'"

This is not working as the toString doesn’t output the date in a format that my SQL server understands. I would like to have the conditional still use a prepared statement with the jdbc driver and toss the parameter in, I just want the prepared statement to be dependent on if the parameter is null or not. Can this be done?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T05:13:25+00:00Added an answer on June 11, 2026 at 5:13 am

    Before you have used the $P!{} expression the JDBC-Driver does all formatting for you.

    But if you use the $P!{} expression you have to format yourself.

    Something like this should work:

    (
    $P{some.date} == null 
    ? 
    "" 
    : 
    "AND some_date >'" + (new SimpleDateFormat("dd.MM.yyyy HH:mm:ss.SSS")).format($P{some.date}) + "'"
    )
    

    Depending on your data type you have to customize dd.MM.yyyy HH:mm:ss.SSS.

    If you don’t want to use the $P!{} expression you can avoid it with the solution below.

    I personally don’t like this way. It also may cause a bad execution plan.

    If don’t want to use $P!{} because you worry about sql injection. It’s needless as long your parameter $P{some.date} contains a safe data type like java.lang.Date.


    Create a parameter. Let’s call it ${is_null_pram} and add a default expression with param class Integer:

    ($P{some.date} == null ? 1 : 0)
    

    Now you can query:

    SELECT 
        * 
    FROM foo 
    WHERE
        bar = $P{bar}
        AND
            (
                some_date > $P{some.date}
                OR 1 = $P{is_null_pram}
            )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say that I have a date in R and it's formatted as follows.
let's say I have a select list as follows: <select name='languages'> <option value='german'>German</option> <option
Let's say I don't have photoshop, but I want to make pattern files (.pat)
Let's say I have a method in java, which looks up a user in
Let me explain best with an example. Say you have node class that can
Let's say that I have a SQLite database that I create in a separate
Let's say I have thousands of users and I want to make the passwords
Let's say I have an facebook application running using the JS SDK. First user
Let's say I have multiple requirements for a password. The first is that the
Let's assume that a user votes for some movies in a scale of 1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.