Sunday, June 28, 2009

Oracle seconds to hours minutes seconds

This code converts seconds to hours, minutes, and seconds.

select to_char(trunc(/60/60),'09') ||
to_char(trunc(mod(,3600)/60),'09') ||
to_char(mod(mod(,3600),60),'09')
from dual;
I use SQL*Plus to show how to use it:

SQL> variable n number

SQL> exec :n := 3000;

PL/SQL procedure successfully completed.

SQL> select to_char(trunc(:n/60/60),'09') ||
2 to_char(trunc(mod(:n,3600)/60),'09') ||
3 to_char(mod(mod(:n,3600),60),'09')
4 from dual;

TO_CHAR(T
---------
00 50 00

SQL> exec :n := 45

PL/SQL procedure successfully completed.

SQL> /

TO_CHAR(T
---------
00 00 45

SQL> exec :n := 3770;

PL/SQL procedure successfully completed.

SQL> /

TO_CHAR(T
---------
01 02 50

No comments:

Post a Comment