How can I select records ONLY from yesterday?

后端 未结 6 2161
借酒劲吻你
借酒劲吻你 2020-12-15 03:05

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_d         


        
相关标签:
6条回答
  • 2020-12-15 03:16

    If you want the timestamp for yesterday try something like:

    (CURRENT_TIMESTAMP - INTERVAL '1' DAY)
    
    0 讨论(0)
  • 2020-12-15 03:19

    This comment is for readers who have found this entry but are using mysql instead of oracle! on mysql you can do the following: Today

    SELECT  * 
    FROM 
    WHERE date(tran_date) = CURRENT_DATE()
    

    Yesterday

    SELECT  * 
    FROM yourtable 
    WHERE date(tran_date) = DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY)
    
    0 讨论(0)
  • 2020-12-15 03:20

    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.

    0 讨论(0)
  • 2020-12-15 03:21
    to_char(tran_date, 'yyyy-mm-dd') = to_char(sysdate-1, 'yyyy-mm-dd')
    
    0 讨论(0)
  • 2020-12-15 03:23

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

    AND oh.tran_date >= trunc(sysdate-1)
    
    0 讨论(0)
  • 2020-12-15 03:30
    trunc(tran_date) = trunc(sysdate -1)
    
    0 讨论(0)
提交回复
热议问题