我有这样的SQL:
SELECT EMP_NAME, DEPT FROM EMPLOYEE WHERE TIME_CREATED >= TO_DATE('26/JAN/2011','dd/mon/yyyy')
->这将返回10行,并且TIME_CREATED = ‘26 -JAN-2011’
现在,当我这样做时,我什么也没回来
SELECT EMP_NAME, DEPT FROM EMPLOYEE WHERE TIME_CREATED = TO_DATE('26/JAN/2011','dd/mon/yyyy')
->采取大于大于
有什么原因吗?
是:TIME_CREATED包含日期和 时间 。使用TRUNC剥离时间:
TRUNC
SELECT EMP_NAME, DEPT FROM EMPLOYEE WHERE TRUNC(TIME_CREATED) = TO_DATE('26/JAN/2011','dd/mon/yyyy')
更新: 正如Dave Costa在下面的注释中指出的那样,这将防止Oracle使用该列的索引(TIME_CREATED如果存在)。没有此问题的另一种方法是:
TIME_CREATED
SELECT EMP_NAME, DEPT FROM EMPLOYEE WHERE TIME_CREATED >= TO_DATE('26/JAN/2011','dd/mon/yyyy') AND TIME_CREATED < TO_DATE('26/JAN/2011','dd/mon/yyyy') + 1