Oracle stores dates in a 7- or 8-byte data structure which always includes a time component.
When you have a date TO_DATE( '21-JUN-2016', 'DD-MON-YYYY' )
then Oracle will create the date with the time 00:00:00
.
If you do:
WHERE TRAN_DATETIME BETWEEN TO_DATE( '21-JUN-2016 00:00:00', 'DD-MON-YYYY HH24:MI:SS' )
AND TO_DATE( '21-JUN-2016 00:00:00', 'DD-MON-YYYY HH24:MI:SS' )
Then you will only get results which are on the day you want and have the time component 00:00:00
.
Now you can use TRUNC()
to set the time component to 00:00:00
:
WHERE TRUNC( TRAN_DATETIME ) BETWEEN DATE '2016-06-21' AND DATE '2016-06-21'
However, if there is an index on the TRAN_DATETIME
column then this query will not use it (it could, however, use a function-based index on TRUNC( TRAN_DATETIME )
).
A solution that can use the index is:
WHERE TRAN_DATETIME >= :start_date
AND TRAN_DATE < :end_date + INTERVAL '1' DAY
(Assuming that :start_date
and :end_date
are bind variable passed in with a time component of 00:00:00
- which is what you appear to be doing).
Incorporating the NVL
statements:
WHERE ( :start_date IS NULL OR TRAN_DATETIME >= :start_date )
AND ( :end_date IS NULL OR TRAN_DATE < :end_date + INTERVAL '1' DAY )