Oracle SQL comparing dates

后端 未结 1 541
隐瞒了意图╮
隐瞒了意图╮ 2021-01-07 01:01
SQL> select * from porder ;

   OID       BILL ODATE
  ------ ---------- ---------
    10        200 06-OCT-13
     4      39878 05-OCT-13
     5     430000 05-OC         


        
相关标签:
1条回答
  • 2021-01-07 01:33

    In Oracle, a DATE always has a time component. Your client may or may not display the time component, but it is still there when you try to do an equality comparison. You also always want to compare dates with dates rather than strings which use the current session's NLS_DATE_FORMAT for doing implicit conversions thus making them rather fragile. THat will involve either ANSI date literals or explicit to_date calls

    You can use the TRUNC function to truncate the DATE to midnight

    SELECT *
      FROM porder
     WHERE trunc(odate) = date '2013-10-04'
    

    Or you can do a range comparison (which will be more efficient if you can benefit from an index on odate)

    SELECT *
      FROM porder
     WHERE odate >= to_date( '04-Oct-2013', 'DD-Mon-YYYY' )
       AND odate <  to_date( '05-Oct-2013', 'DD-Mon-YYYY' );
    
    0 讨论(0)
提交回复
热议问题