Where 子句中的 Oracle DateTime?
我有这样的 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')
->拿出比拿出来更大的东西
有什么理由吗?
I have sql something like this:
SELECT EMP_NAME, DEPT
FROM EMPLOYEE
WHERE TIME_CREATED >= TO_DATE('26/JAN/2011','dd/mon/yyyy')
-> This returns 10 rows and TIME_CREATED = '26-JAN-2011'
Now when i do this i don't get any rows back,
SELECT EMP_NAME, DEPT
FROM EMPLOYEE
WHERE TIME_CREATED = TO_DATE('26/JAN/2011','dd/mon/yyyy')
-> Took the greater than out
Any reason why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是:TIME_CREATED 包含日期和时间。使用
TRUNC
删除时间:更新:
正如 Dave Costa 在下面的评论中指出的那样,这将阻止 Oracle 使用
TIME_CREATED
列的索引(如果存在)。没有此问题的替代方法是:Yes: TIME_CREATED contains a date and a time. Use
TRUNC
to strip the time:UPDATE:
As Dave Costa points out in the comment below, this will prevent Oracle from using the index of the column
TIME_CREATED
if it exists. An alternative approach without this problem is this:您还可以使用以下命令在查询中包含 TIME 部分:
You can also use the following to include the TIME portion in your query:
你还可以这样做:
You could also do:
这是因为 Oracle 中的
DATE
列也包含时间部分。to_date()
函数的结果是时间设置为00:00:00
的日期,因此它可能与表中的任何行都不匹配。你应该使用:
This is because a
DATE
column in Oracle also contains a time part. The result of theto_date()
function is a date with the time set to00:00:00
and thus it probably doesn't match any rows in the table.You should use:
正如其他人上面评论的那样,使用 TRUNC 将阻止使用索引(如果 TIME_CREATED 上有索引)。为了避免这个问题,查询可以构造为
86399,比一天的秒数少 1 秒。
As other people have commented above, using TRUNC will prevent the use of indexes (if there was an index on TIME_CREATED). To avoid that problem, the query can be structured as
86399 being 1 second less than the number of seconds in a day.