Question

I've got a report I'm writing in BIRT against an Oracle database:

Table:

tranx_no (string)
type (string)
description (string)
amount (number(14,2))
date (date)

Query in BIRT:

SELECT tranx_no, type, description, amount
FROM tranx_table
WHERE date BETWEEN ? AND ?

If I just do plain dates (02-01-2014 and 02-14-2014) in the parameters, it misses things that happen during the day of the 14th (stops at midnight). I've tried concatenating the time onto the date parameter

WHERE date BETWEEN ? || '12:00:00 AM' AND ? || '11:59:59 PM'

and got an ORA 01843 error. I also tried casting it with to_date

WHERE date BETWEEN TO_DATE(? || '12:00:00 AM', 'MM-DD-YYYY HH:MI:SS AM') AND TO_DATE(? || '11:59:59 PM', 'MM-DD-YYYY HH:MI:SS AM')

and no joy there either. ORA 01847 error happens with that one.

Ideas? I know there's probably something simple I'm not thinking of, but Google hasn't helped. I'm wanting to edit the query, not change the date entry on the face of the form.

Thanks.

Was it helpful?

Solution

Correct handling DATEs with BIRT can be tricky.

I recommend to always use VARCHAR2 for passing DATE parameters (for report parameters as well as for query parameters). Always verify the data type of your parameters.

In your SQL, always use TO_DATE / TO_CHAR with an explicit date format - otherwise the results would depend on the locale settings.

Next, be sure that the value the user entered is adjusted to a known date-format before it is used in the query. For example, in Germany the SQL date format DD.MM.YYYY HH24:MI:SS is commonly used.

You could create a utility function which adds the missing parts (e.g. the time) automatically. Use an additional argument in this function to specify if it's a "from" date (adds 00:00:00) or a "to" date (adds 23:59:59).

OTOH if the UI forces the user to enter a from-to date without time (say in the format 'MM-DD-YYYY' as in your example), you could just code

WHERE (date >= to_date(?, 'MM-DD-YYYY') and date < to_date(?, 'MM-DD-YYYY') + 1)

Note the usage of < and not <=.

This works because if no time is specified in the format mask, 00:00:00 (in HH24:MI:SS format) is implied.

OTHER TIPS

As you pointed out the start date is not a problem as it begins at 00:00:00 of the start date. If your paramter is a text box your users can enter 02-01-2014 08:00:00 to get results starting at 8am on Feb 1.

Note that my date format has the year first, while yours has the year last

For the end date, I use a text box paramater with this as my default value

//Creates a date for one second before midnight of the current date,
//which is properly formatted to use as an end date in quires (for my data)
// Note that a custom date format (yyyy-MM-dd HH:mm:ss) is required for proper display in pop-up GUI

var T; T = BirtDateTime.addDay(BirtDateTime.today(),1);
var Y; Y = BirtDateTime.year(T);
var M; M = BirtDateTime.month(T);
var D; D = BirtDateTime.day(T);

{
Y +"-"
+ M +"-"
+ D +" "
+"00:00:00"
}

I also use this help text

Enter date as YYYY-MM-DD. For example, 2013-3-14

Adn this prompt text

End Date (YYYY-MM-DD), if time is blank will default to 00:00:00
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top