Time Function used to calculate total Time between two time given in parameter

 The following function will calculate total seconds getting parameter of two time values:

CREATE OR REPLACE FUNCTION F_TIME (VTIMEIN VARCHAR2,VTIMEOUT VARCHAR2) RETURN number IS
  VSEC_IN        NUMBER(10);
  VSEC_OUT       NUMBER(10);
  VHH            VARCHAR2(8);
  VMM            VARCHAR2(8);
  VSS            VARCHAR2(8);
  V_TOT_WORKED   NUMBER(10);
BEGIN
  VSEC_IN :=TO_NUMBER(SUBSTR(VTIMEIN,1,2))*60*60+TO_NUMBER(SUBSTR(VTIMEIN,4,2))*60+TO_NUMBER(SUBSTR(VTIMEIN,7,2));
  VSEC_OUT:=TO_NUMBER(SUBSTR(VTIMEOUT,1,2))*60*60+TO_NUMBER(SUBSTR(VTIMEOUT,4,2))*60+TO_NUMBER(SUBSTR(VTIMEOUT,7,2));
  V_TOT_WORKED:=VSEC_OUT-VSEC_IN;
  RETURN V_TOT_WORKED;
END;
/
 

 
The following function will calculated time duration output in HH:MI:SS format:
 
CREATE OR REPLACE FUNCTION F_TIMEWORKED
   (VTIMEIN VARCHAR2,VTIMEOUT VARCHAR2)
   RETURN VARCHAR2 IS
   VSEC_IN                 NUMBER(10);
   VSEC_OUT                NUMBER(10);
   VHH                     VARCHAR2(8);
   VMM                     VARCHAR2(8);
   VSS                     VARCHAR2(8);
   V_TOT_WORKED    NUMBER(10);
   V_WORKED                VARCHAR2(8);
BEGIN
   VSEC_IN :=TO_NUMBER(SUBSTR(VTIMEIN,1,2))*60*60+TO_NUMBER(SUBSTR(VTIMEIN,4,2))*60+TO_NUMBER(SUBSTR(VTIMEIN,7,2));
   VSEC_OUT:=TO_NUMBER(SUBSTR(VTIMEOUT,1,2))*60*60+TO_NUMBER(SUBSTR(VTIMEOUT,4,2))*60+TO_NUMBER(SUBSTR(VTIMEOUT,7,2));
   V_TOT_WORKED:=VSEC_OUT-VSEC_IN;
   VHH:=TRUNC(V_TOT_WORKED/(60*60));
   VMM:=TRUNC((V_TOT_WORKED-VHH*60*60)/60);
   VSS:=TRUNC(V_TOT_WORKED-((VHH*60*60)+VMM*60));
   V_WORKED:=LPAD(VHH,2,'0')||':'||LPAD(VMM,2,'0')||':'||LPAD(VSS,2,'0');
   RETURN V_WORKED;
END;
/
 
SELECT F_TIMEWORKED('10:00:00','18:05:00') FROM DUAL;
 
 
 
 


Muhammad Abdullah Al Noor

Muhammad Abdullah Al Noor, An Oracle Apex Consultants and founder of Noors Technology (www.noorstech.com). Core Expertise : Database Administration, Oracle Forms and Reports Development, Oracle Apex Application Designer and Development, Linux Professional etc. Also the owner of TrainerBD Training and OraDemy E-Learning. WhatsApp +8801790721177

Post a Comment

Previous Post Next Post