一尘不染

Oracle DateTime在哪里子句?

sql

我有这样的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')

->采取大于大于

有什么原因吗?


阅读 137

收藏
2021-03-17

共1个答案

一尘不染

是:TIME_CREATED包含日期和 时间 。使用TRUNC剥离时间:

SELECT EMP_NAME, DEPT
FROM EMPLOYEE
WHERE TRUNC(TIME_CREATED) = TO_DATE('26/JAN/2011','dd/mon/yyyy')

更新:
正如Dave Costa在下面的注释中指出的那样,这将防止Oracle使用该列的索引(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
2021-03-17