Oracle Time Comparisons

 ̄綄美尐妖づ 提交于 2019-12-12 16:19:04

问题


Is Oracle (10g) doing a proper TIME comparison here, or do I need to convert to decimal time and then compare? E.g.,

IF (SELECT TO_CHAR(sysdate,'HH24:MI:SS') from dual) <= '15:00'
THEN (...)

Thanks.


回答1:


IF (sysdate <= trunc(sysdate)+15/24)
THEN (...)

should do the trick...




回答2:


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,

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.

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!




回答3:


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 
(....)


来源:https://stackoverflow.com/questions/9625323/oracle-time-comparisons

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!