Oracle:仅按小时比较日期
我们有一个表,其中有 DATE 列 d。
我想获取 d 列大于/小于某个值的所有行,无论日期如何。
例如
| d |
-------------------
|2009/11/1 15:55:23|
--------------------
|2009/11/2 15:55:23|
--------------------
|2009/11/3 15:55:23|
--------------------
|2009/11/3 17:55:23|
--------------------
,如果我想要在下午 5 点之后标记所有记录:
select d
from my_table
where extract( hour from d ) > TO_DATE ('17:00:00','HH24:MI:SS')
这应该只返回一条记录
|2009/11/3 17:55:23|
,我不知道这是否是最好的方法,但我在提取函数上收到错误:
ORA-30076: invalid extract field for extract source
Cause: The extract source does not contain the specified extract field.
是否有更好的方法来做到这一点? 那个错误是怎么回事?提取仅适用于 sysdate,就像我发现的所有示例一样?
提前致谢
We have a table which has a DATE column d.
I'd like to get all the rows where the d column is greater / lower than some value, regardless of the date.
For example
| d |
-------------------
|2009/11/1 15:55:23|
--------------------
|2009/11/2 15:55:23|
--------------------
|2009/11/3 15:55:23|
--------------------
|2009/11/3 17:55:23|
--------------------
For example, If I want all the records marked after 5 PM:
select d
from my_table
where extract( hour from d ) > TO_DATE ('17:00:00','HH24:MI:SS')
This should return only one record
|2009/11/3 17:55:23|
I don't know if this is the best way to do it, but I get an error on the extract function:
ORA-30076: invalid extract field for extract source
Cause: The extract source does not contain the specified extract field.
Is there a better way to do this?
Whats up with that error? extract only available for sysdate, as in all examples i've found?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这个怎么样?
How about this?
目前没有可供测试的 Oracle 数据库,但我认为以下应该可行:
但我不明白为什么需要 CAST 此页面 表示 HOUR 是 DATE 中的有效字段。
分享并享受。
Don't have an Oracle database to test with at the moment but I think the following should work:
But I don't understand why the CAST would be needed as this page says that HOUR is a valid field in a DATE.
Share and enjoy.
试试这个:
Try this: