Skip to content
Advertisement

ORACLE DATETIME ROUND UP WHEN MINUTES 0

I have a datetime:

select to_date(to_char(r.DATETIMEUTC,'YYYY-MM-DD HH24:MI:SS'),'YYYY-MM-DD HH24:MI:SS') as DATETIME_UTC,
       round(r.DATETIMEUTC, 'hh') as rounded
from   PROD.WX_RAW r;

I need to round up just when the minutes are different to 00.

Example:

1/1/2000 2:37:00 AM => should be => 1/1/2000 3:00:00 AM. (dont round up if hour =00) 1/1/2000 2:00:00 AM => should be => 1/1/2000 2:00:00 AM

For now my code has round function but this one look the nearest hour, I need even if minutes are 15, round up the hour, except when the minutes are 00 in that case I need to keep the hour.

Regards

Advertisement

Answer

I’d truncate the date to the hour and then use a case statement to decide whether to add an hour.

trunc( r.DATETIMEUTC, 'HH24' ) -- truncate to the hour
+ case when to_number( to_char( r.DATETIMEUTC, 'HH24' ) ) != 0
       then interval '1' hour
       else null
    end
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement