oracle数据库2个时间段的时间差函数

create or replace   
FUNCTION DATEDIFF (date1 IN DATE,date2 IN DATE) RETURN NUMBER     
IS   
datesx NUMBER(10);   
secondsx NUMBER(20);   
stra VARCHAR2(19);   
strb VARCHAR2(19);   
hh NUMBER(2);   
mi NUMBER(2);   
ss NUMBER(2);   
BEGIN   
stra := to_char(date1,'yyyy-mm-dd hh24:mi:ss'); -- 2010-12-12 00:00:00   
strb := to_char(date2,'yyyy-mm-dd hh24:mi:ss');   
hh := cast(substr(stra,12,2) as NUMBER) - cast(substr(strb,12,2) as NUMBER);   
mi := cast(substr(stra,15,2) as NUMBER) - cast(substr(strb,15,2) as NUMBER);   
ss := cast(substr(stra,18,2) as NUMBER) - cast(substr(strb,18,2) as NUMBER);   
datesx := (to_date(substr(stra,1,10),'yyyy-mm-dd') - to_date(substr(strb,1,10),'yyyy-mm-dd'));   
IF(datesx = 0) THEN   
    secondsx := hh*3600 + mi*60 + ss;   
ELSE   
    secondsx := datesx*24*3600 + hh*3600 + mi*60 + ss;   
END IF;   
RETURN secondsx;   
END DATEDIFF; 

你可能感兴趣的:(oracle,sql)