Oracle timing comparison

Is Oracle (10g) the correct TIME choice here, or do I need to convert to decimal time and then compare? For example.

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

      

Thank.

+3


source to share


3 answers


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

      



gotta do the trick ...

+10


source


You cannot do select

in the instructions if

, but you can do a direct comparison with sysdate. If you are doing it this way, it is probably better to use a number rather than relying on implicit conversion. You also don't need extra minutes, etc. Something like,

begin

   if to_number(to_char(sysdate,'HH24')) <= 15 then
      -- do something
   end if;

end;

      

If you want to use minutes by converting it to a string without a colon, you can do a more direct comparison. While the date / time is converted in 24-hour format without additional settings, and in the opposite way, comparing year, month, day, hour, etc. will always be accurate, eg.



begin

   if to_char(sysdate,'HH24MI') <= '1515' then
      -- do something
   end if;

end;

      

However, it is often better to do date comparisons as @cagcowboy just posted before I get there!

+4


source


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


source







All Articles