Calculate Difference Between Dates in SQL*Plus or PL/SQL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bradleyc
    New Member
    • Aug 2005
    • 12

    #1

    Calculate Difference Between Dates in SQL*Plus or PL/SQL

    How would you calculate the difference between two dates?
  • richasaraf
    New Member
    • Aug 2005
    • 23

    #2
    Procedure to find difference between dates !

    Originally posted by bradleyc
    How would you calculate the difference between two dates?
    Use following procedure... which i got from one site ....
    it will help u....

    Code:
     CREATE OR REPLACE 
    PROCEDURE PRINT_DATE_DIFF(p_dte1 IN DATE, p_dte2 IN DATE)
    IS
    v_diff NUMBER := 0;
    v_hrs NUMBER := 0;
    v_min NUMBER := 0;
    v_sec NUMBER := 0;
    BEGIN
    v_diff := ABS(p_dte2 - p_dte1);
    v_hrs := TRUNC(v_diff, 0)*24; -- start with days portion if any
    v_diff := (v_diff - TRUNC(v_diff, 0))*24; -- lop off whole days, convert
    --to hrs
    v_hrs := v_hrs + TRUNC(v_diff, 0); -- add in leftover hrs if any
    v_diff := (v_diff - TRUNC(v_diff, 0))*60; -- lop off hrs, convert to mins
    v_min := TRUNC(v_diff, 0); -- whole mins
    v_sec := TRUNC((v_diff - TRUNC(v_diff, 0))*60, 0); -- lop off mins,
    --convert to secs
    DBMS_OUTPUT.put_line(
    TO_CHAR(v_hrs) || ' HRS ' ||
    TO_CHAR(v_min) || ' MIN ' ||
    TO_CHAR(v_sec) || ' SEC');
    END print_date_diff;
    Last edited by Niheel; Dec 6 '05, 07:07 AM.

    Comment

    Working...