Thursday, May 16, 2013

Get the difference between two datetimes

Step1 : Create A SQL Function

  Create Or Replace Function Date2Diff( P_What In Varchar2,
                                         P_D1   In Date,
                                          P_D2   In Date ) Return Number
     As
         L_Result    Number;
     Begin
         Select (P_D2-P_D1) *
                Decode( Upper(P_What),
                        'SS', 24*60*60, 'MI', 24*60, 'HH', 24, Null )
        Into L_Result From Dual;

       Return L_Result;
   End;
   /

Step2 :  Create A Temporary View

  Create Or Replace View Temp_View
     As
    Select To_Date('01-JAN-1999 12:02:04', 'dd-mon-yyyy hh24:mi:ss' ) D1,
            To_Date('15-MAR-1999 01:34:23', 'dd-mon-yyyy hh24:mi:ss' ) D2
     From Dual

Step 3:   Get the Query and Pass The Values

/* Formatted on 2013/05/17 10:25 (Formatter Plus v4.8.8) */
SELECT date2diff ('mi', to_date('01-mar-2013 06:00:00','dd-mon-yyyy hh24:mi:ss' ), to_date('01-mar-2013 08:35:00','dd-mon-yyyy hh24:mi:ss' )) minutes
  FROM temp_view;
o/p: 155



This is The Guide From

http://asktom.oracle.com/pls/asktom/ASKTOM.download_file?p_file=6551242712657900129

No comments:

Post a Comment