Search code examples
sqloracleoracle-sqldeveloper

Same working query doesn't work when comparing dates


I have this:

where
    (trunc(my_date) >= (trunc(to_date('06/03/2020','MM/DD/YYYY')) + INTERVAL '13:00:00' HOUR TO SECOND))
  AND
    (trunc(my_date) < (trunc(to_date('06/03/2020','MM/DD/YYYY')) + INTERVAL '15:00:00' HOUR TO SECOND))

The weird thing is that the bottom part works as expected and the top one doesn't.

I'm trying to get this value: 2020-06-03 13:42:31.862930

The funny thing is that if I change the one above the AND to 06/02/2020, it would include it... What's going on here?


Solution

  • Remove trunc function:

    SQL> select * from t;
    
    MY_DATE
    ---------------------------------------------------------------------------
    2020-06-03 13:42:31.862930
    
    
    SQL> select trunc(my_date) from t;
    
    TRUNC(MY_DATE)
    -------------------
    2020-06-03 00:00:00
    
    
    SQL> --
    SQL> select * from t
      2  where
      3      ((my_date) >= (trunc(to_date('06/03/2020','MM/DD/YYYY')) + INTERVAL '13:00:00' HOUR TO SECOND))
      4    AND
      5      ((my_date) < (trunc(to_date('06/03/2020','MM/DD/YYYY')) + INTERVAL '15:00:00' HOUR TO SECOND))
      6  ;
    
    MY_DATE
    ---------------------------------------------------------------------------
    2020-06-03 13:42:31.862930