Oracle Convert Seconds to Hours:Minutes:Seconds

前端 未结 14 1111
再見小時候
再見小時候 2020-12-08 06:01

I have a requirement to display user available time in Hours:Minutes:Seconds format from a given total number of seconds value. Appreciate if you know a ORACLE function to d

相关标签:
14条回答
  • 2020-12-08 06:30

    Convert minutes to hour:min:sec format

    SELECT 
       TO_CHAR(TRUNC((MINUTES * 60) / 3600), 'FM9900') || ':' ||
       TO_CHAR(TRUNC(MOD((MINUTES * 60), 3600) / 60), 'FM00') || ':' ||
       TO_CHAR(MOD((MINUTES * 60), 60), 'FM00') AS MIN_TO_HOUR FROM DUAL
    
    0 讨论(0)
  • 2020-12-08 06:31

    If you're just looking to convert a given number of seconds into HH:MI:SS format, this should do it

    SELECT 
        TO_CHAR(TRUNC(x/3600),'FM9900') || ':' ||
        TO_CHAR(TRUNC(MOD(x,3600)/60),'FM00') || ':' ||
        TO_CHAR(MOD(x,60),'FM00')
    FROM DUAL
    

    where x is the number of seconds.

    0 讨论(0)
  • 2020-12-08 06:31

    The following is Yet Another Way (tm) - still involves a little calculation but provides an example of using EXTRACT to pull the individual fields out of an INTERVAL:

    DECLARE 
      SUBTYPE BIG_INTERVAL IS INTERVAL DAY(9) TO SECOND;
    
      i        BIG_INTERVAL;
      nSeconds NUMBER := 86400000;
    
      FUNCTION INTERVAL_TO_HMS_STRING(inv IN BIG_INTERVAL)
        RETURN VARCHAR2
      IS
        nHours    NUMBER;
        nMinutes  NUMBER;
        nSeconds  NUMBER;
        strHour_format  VARCHAR2(10) := '09';
        workInv   INTERVAL DAY(9) TO SECOND(9);
      BEGIN
        nHours := EXTRACT(HOUR FROM inv) + (EXTRACT(DAY FROM inv) * 24);
        strHour_format := TRIM(RPAD(' ', LENGTH(TRIM(TO_CHAR(ABS(nHours)))), '0') || '9');
    
        nMinutes := ABS(EXTRACT(MINUTE FROM inv));
        nSeconds := ABS(EXTRACT(SECOND FROM inv));
    
        RETURN TRIM(TO_CHAR(nHours, strHour_format)) || ':' ||
               TRIM(TO_CHAR(nMInutes, '09')) || ':' ||
               TRIM(TO_CHAR(nSeconds, '09'));
      END INTERVAL_TO_HMS_STRING;
    
    BEGIN
      i := NUMTODSINTERVAL(nSeconds, 'SECOND');
    
      DBMS_OUTPUT.PUT_LINE('i (fields) = ' || INTERVAL_TO_HMS_STRING(i));
    END;
    

    The code which extracts the fields, etc, still has to contain a calculation to convert the DAY field to equivalent hours, and is not the prettiest, but wrapped up neatly in a procedure it's not too bad to use.

    Share and enjoy.

    0 讨论(0)
  • 2020-12-08 06:33

    If you have a variable containing f.e. 1 minute(in seconds), you can add it to the systimestamp then use to_char to select the different time parts from it.

    select to_char(systimestamp+60/(24*60*60), 'yyyy.mm.dd HH24:mi:ss') from dual
    
    0 讨论(0)
  • 2020-12-08 06:33

    My version. Show Oracle DB uptime in format DDd HHh MMm SSs

    select to_char(trunc((((86400*x)/60)/60)/24)) || 'd ' ||
       to_char(trunc(((86400*x)/60)/60)-24*(trunc((((86400*x)/60)/60)/24)), 'FM00') || 'h ' ||
       to_char(trunc((86400*x)/60)-60*(trunc(((86400*x)/60)/60)), 'FM00') || 'm ' ||
       to_char(trunc(86400*x)-60*(trunc((86400*x)/60)), 'FM00') || 's' "UPTIME"
     from (select (sysdate - t.startup_time) x from V$INSTANCE t);
    

    idea from Date / Time Arithmetic with Oracle 9/10

    0 讨论(0)
  • 2020-12-08 06:35

    The following code is less complex and gives the same result. Note that 'X' is the number of seconds to be converted to hours.

    In Oracle use:

    SELECT TO_CHAR (TRUNC (SYSDATE) + NUMTODSINTERVAL (X, 'second'),
                    'hh24:mi:ss'
                   ) hr
      FROM DUAL;
    

    In SqlServer use:

    SELECT CONVERT(varchar, DATEADD(s, X, 0), 108);
    
    0 讨论(0)
提交回复
热议问题