Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

DATE DIFF FUNCTION

Ricky007Apr 18 2017 — edited Apr 22 2017

Dear experts ,

                 here is my function to calculate difference between two dates.

create or replace

Function  hour_diff

(p_p_StartDt TIMESTAMP,

p_p_EndDt TIMESTAMP)

Return Varchar2

As

v_TotalSec number(10);

v_DiffSecs number(10);

  v_m_DaysHour Varchar2(15);

Begin

--SELECT 24*60*60 INTO v_TotalSec FROM DUAL;

--SELECT (p_p_EndDt - p_p_StartDt) * 86400 INTO v_DiffSecs FROM DUAL;

       

   

  select days || 'd' || ' '||

hours || 'h' || ' '||

minutes || 'm' into v_m_DaysHour

from(

select extract( day from diff ) days,

           extract( hour from diff ) hours,

           extract( minute from diff ) minutes

           from (select to_timestamp( p_p_StartDt, 'yyyy-mm-dd' ) - to_timestamp( p_p_EndDt, 'yyyy-mm-dd' ) diff

             from dual));

 

Return    v_m_DaysHour;

End;

op required

---------

1d 2h 40m

tried function

select hour_diff('18-04-17 07:48:25','17-04-17 07:48:25') from dual;

ORA-01843: not a valid month

01843. 00000 -  "not a valid month"

*Cause:   

*Action:

PLEASE HELP

This post has been answered by Paulzip on Apr 18 2017
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 20 2017
Added on Apr 18 2017
18 comments
3,543 views