Oracle Convert Seconds to Hours:Minutes:Seconds

前端 未结 14 1110
再見小時候
再見小時候 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:17

    For greater than 24 hours you can include days with the following query. The returned format is days:hh24:mi:ss

    Query:
    select trunc(trunc(sysdate) + numtodsinterval(9999999, 'second')) - trunc(sysdate) || ':' || to_char(trunc(sysdate) + numtodsinterval(9999999, 'second'), 'hh24:mi:ss') from dual;

    Output:
    115:17:46:39

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

    For the comment on the answer by vogash, I understand that you want something like a time counter, thats because you can have more than 24 hours. For this you can do the following:

    select to_char(trunc(xxx/3600)) || to_char(to_date(mod(xxx, 86400),'sssss'),':mi:ss') as time
    from dual;
    

    xxx are your number of seconds.

    The first part accumulate the hours and the second part calculates the remaining minutes and seconds. For example, having 150023 seconds it will give you 41:40:23.

    But if you always want have hh24:mi:ss even if you have more than 86000 seconds (1 day) you can do:

    select to_char(to_date(mod(xxx, 86400),'sssss'),'hh24:mi:ss') as time 
    from dual;
    

    xxx are your number of seconds.

    For example, having 86402 seconds it will reset the time to 00:00:02.

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

    Assuming your time is called st.etime below and stored in seconds, here is what I use. This handles times where the seconds are greater than 86399 seconds (which is 11:59:59 pm)

    case when st.etime > 86399 then to_char(to_date(st.etime - 86400,'sssss'),'HH24:MI:SS') else to_char(to_date(st.etime,'sssss'),'HH24:MI:SS') end readable_time

    0 讨论(0)
  • 2020-12-08 06:23
    create or replace procedure mili(num in number)
    as
    yr number;
    yrsms number;
    mon number;
    monsms number;
    wk number;
    wksms number;
    dy number;
    dysms number;
    hr number;
    hrsms number;
    mn number;
    mnsms number;
    sec number;
    begin 
    yr := FLOOR(num/31556952000);
    yrsms := mod(num, 31556952000);
    mon := FLOOR(yrsms/2629746000);
    monsms := mod(num,2629746000);
    wk := FLOOR(monsms/(604800000));
    wksms := mod(num,604800000); 
    dy := floor(wksms/ (24*60*60*1000));
    dysms :=mod(num,24*60*60*1000);
    hr := floor((dysms)/(60*60*1000));
    hrsms := mod(num,60*60*1000);
    mn := floor((hrsms)/(60*1000));
    mnsms := mod(num,60*1000);
    sec := floor((mnsms)/(1000));
    dbms_output.put_line(' Year:'||yr||' Month:'||mon||' Week:'||wk||' Day:'||dy||' Hour:'||hr||' Min:'||mn||' Sec: '||sec);
    end;
    /
    
    
    begin 
    mili(12345678904234);
    end;
    
    0 讨论(0)
  • 2020-12-08 06:23
    create or replace function `seconds_hh_mi_ss` (seconds in number)     
    return varchar2
    is
    hours_var number;    
    minutes_var number;    
    seconds_var number;    
    remeinder_var number;    
    output_var varchar2(32);    
    begin    
    select seconds - mod(seconds,3600) into hours_var from dual;    
    select seconds - hours_var into remeinder_var from dual;    
    select (remeinder_var - mod(remeinder_var,60)) into minutes_var from dual;    
    select seconds - (hours_var+minutes_var) into seconds_var from dual;    
    output_var := hours_var/3600||':'||minutes_var/60||':'||seconds_var;    
    return(output_var);    
    end;
    /
    
    0 讨论(0)
  • 2020-12-08 06:25

    Try this one. Very simple and easy to use

    select to_char(to_date(10000,'sssss'),'hh24:mi:ss') from dual;
    
    0 讨论(0)
提交回复
热议问题