ZEE
ZEE

Reputation: 391

Convert minutes to HH24:MI format

Can you please help in converting minutes to the format of ('HH24:MI').

I am getting the result in integer which is the minutes. So, how to convert them?

Thanks

Upvotes: 4

Views: 18981

Answers (3)

Jean Paulo
Jean Paulo

Reputation: 21

This can save you, but I had problems with time greater than 1440 minutes.

select to_char(trunc(sysdate) + [MINUTES]/24/60, 'hh24:mi') from dual;

So I did a function that verifies if minute is greater or equal 1440:

IF (QT_MINUTES_P IS NOT NULL) THEN
   IF (QT_MINUTES_P >= 1440) THEN
         SELECT  ROUND(QT_MINUTES_P/ 60) ||':'|| MOD(QT_MINUTES_P, 60)
         INTO    DS_RESULT
         FROM    DUAL;
     ELSE
       SELECT   TO_CHAR(TRUNC(SYSDATE) + (QT_MINUTES_P)/24/60, 'hh24:mi')
         INTO   DS_RESULT
         FROM   DUAL;
   END IF;
END IF;

Upvotes: 1

Another way:

WITH c AS
  (SELECT 492 AS MINUTES FROM DUAL)
SELECT TRIM(TO_CHAR(TRUNC(MINUTES / 60), '09')) || ':' ||
       TRIM(TO_CHAR(TRUNC(MOD(ABS(MINUTES), 60)), '09')) AS HHMM
  FROM C

This will have issues if the time exceeds 1440 minutes (24 hours) but it gives you something to start with.

Share and enjoy.

Upvotes: 1

René Nyffenegger
René Nyffenegger

Reputation: 40499

Assuming, you want to convert 492 minutes:

select to_char(trunc(sysdate) + 492/24/60, 'hh24:mi') from dual;

If you want a function:

create or replace 
  function tq84_convert_minutes_hh24mm (p_minutes in number) 
  return varchar2 is
begin

    return to_char (trunc(sysdate) + p_minutes / 24/60, 'hh24:mi');

end tq84_convert_minutes_hh24mm;
/

Later ...

begin
  dbms_output.put_line (tq84_convert_minutes_hh24mm(492));
end;
/

Upvotes: 7

Related Questions