Search code examples
sqldatecompareto-dateto-char

Can someone explain why the below queries function differently? Literally the only difference between the two is that


The below two queries are identical except that the first has >= on the last line and the second has =. Yet the first returns 129 rows, while the second returns 0. Today is 11-15-2016, so I expected them to return the same. The data type of date_time is date.

Can someone explain why simply changing the operator from >= to > changes the result? Thanks,

select orderid
    from order_log
    where order_version = 0
    and description = 'Order Complete'
    and date_time **>=** to_date('11-15-2016', 'MM-DD-YYYY')

select orderid
    from order_log
    where order_version = 0
    and description = 'Order Complete'
    and date_time **=** to_date('11-15-2016', 'MM-DD-YYYY')

Solution

  • date_time is a timestamp (date + time) and you don't have events that occurred exactly at midnight (2016-11-15 00:00:00).


    select          sysdate
                   ,case when sysdate = date '2016-11-16' then 'Y' else 'N' end as is_equal
    
     from           dual
    

    SYSDATE             IS_EQUAL
    2016-11-16 00:23:37 N