Oracle SQL comparing dates

纵饮孤独 提交于 2019-12-19 04:45:40

问题


SQL> select * from porder ;

   OID       BILL ODATE
  ------ ---------- ---------
    10        200 06-OCT-13
     4      39878 05-OCT-13
     5     430000 05-OCT-13
    11        427 06-OCT-13
    12        700 06-OCT-13
    14      11000 06-OCT-13
    15      35608 06-OCT-13
    13      14985 06-OCT-13
     8      33000 06-OCT-13
     9        600 06-OCT-13
    16        200 06-OCT-13

   OID       BILL ODATE
------ ---------- ---------
     1        200 04-OCT-13
     2      35490 04-OCT-13
     3       1060 04-OCT-13
     6        595 05-OCT-13
     7        799 05-OCT-13

16 rows selected.  

 SQL> select * from porder where odate='04-Oct-2013';

no rows selected

please someone help...why isn't the query working..??


回答1:


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' );


来源:https://stackoverflow.com/questions/19216076/oracle-sql-comparing-dates

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!