views:

447

answers:

4

I've spent hours searching the web for an answer to this question...

Here's what I currently have:

select  *
from    order_header oh
where   tran_date = sysdate-1

Thanks in advance.

+3  A: 

trunc(tran_date) = trunc(sysdate -1)

Henry Gao
If, as I presume, there's an index on tran_date on which this query relies, calling a funtion on the indexed column like this will decimate the performance of the query.
ninesided
then rexem's method will be helpful.
Henry Gao
In that case you can add a function based index:create index index_name on table_name(trunc(tran_date));
Robert Merkwürdigeliebe
@Robert: IME, most DBAs won't let you. And I have to agree, because it's not really necessary.
OMG Ponies
Henry is right : rexem's solution is probably better (less impact) than adding a functio based index...
Robert Merkwürdigeliebe
A: 

to_char(tran_date, 'yyyy-mm-dd') = to_char(sysdate-1, 'yyyy-mm-dd')

Pablo Santa Cruz
Oracle supports a wide range of operators directly on dates, without having to resort to doing string comparisons.
Jeffrey Kemp
+10  A: 

Use:

AND oh.tran_date BETWEEN TRUNC(SYSDATE - 1) AND TRUNC(SYSDATE) - 1/86400

Reference: TRUNC

Calling a function on the tran_date means the optimizer won't be able to use an index (assuming one exists) associated with it. Some databases, such as Oracle, support function based indexes which allow for performing functions on the data to minimize impact in such situations, but IME DBAs won't allow these. And I agree - they aren't really necessary in this instance.

OMG Ponies
One small caveat-- you may need to subtract 1 second from the upper bound. Otherwise, if you have a TRAN_DATE which is today at midnight, it will be returned in the query for rows inserted yesterday. I wouldn't expect that to be the desired behavior.
Justin Cave
A: 

If you don't support future dated transactions then something like this might work:

AND oh.tran_date >= trunc(sysdate-1)
ninesided