SQL Oracle 时间比较
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9625323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Oracle Time Comparisons
提问by vincent
Is Oracle (10g) doing a proper TIME comparison here, or do I need to convert to decimal time and then compare? E.g.,
Oracle (10g) 是否在此处进行了适当的 TIME 比较,还是需要转换为十进制时间然后进行比较?例如,
IF (SELECT TO_CHAR(sysdate,'HH24:MI:SS') from dual) <= '15:00'
THEN (...)
Thanks.
谢谢。
回答by cagcowboy
IF (sysdate <= trunc(sysdate)+15/24)
THEN (...)
should do the trick...
应该做的伎俩...
回答by Ben
You can't do a select
in an if
statement, but you can do a direct comparison to sysdate. If you're doing it like this it would probably be better to use a number rather than relying on implicit conversion. You also don't need the extra minutes etc. Something like,
您不能select
在if
语句中执行 a ,但可以直接与 sysdate 进行比较。如果您这样做,最好使用数字而不是依赖隐式转换。你也不需要额外的时间等等。比如,
begin
if to_number(to_char(sysdate,'HH24')) <= 15 then
-- do something
end if;
end;
If you did want to use the minutes then by converting it into a string without the colon you can do a more direct comparison. As long as the date / time is converted in 24 hour format without extras and in reverse, year, month, day, hour etc comparisons will always be accurate, e.g.
如果您确实想使用分钟,那么通过将其转换为不带冒号的字符串,您可以进行更直接的比较。只要日期/时间以 24 小时格式转换而没有额外的和反向,年、月、日、小时等比较将始终是准确的,例如
begin
if to_char(sysdate,'HH24MI') <= '1515' then
-- do something
end if;
end;
However, it's often best to do date comparisons as @cagcowboy has just posted before I got there!
但是,通常最好进行日期比较,因为@cagcowboy 在我到达之前刚刚发布了!
回答by Tabish
use the following code
使用以下代码
SELECT TO_CHAR(SYSDATE, 'HH24MI') INTO V_SYS_TIME FROM DUAL; IF V_SYS_TIME BETWEEN V1_TIME AND V2_TIME THEN (....)