[导入]Oracle常用函数:DateDiff()

/*
--功能:将字符串转为年月日格式,删除时分秒.
--来源:http://jorkin.reallydo.com/article.asp?id=529
--参数:
      Datechar Varchar2
--Oracle9i测试通过
*/

Create   Or   Replace   Function  CDate(Datechar  In   Varchar2 Return  Date  Is
    ReallyDo Date;
Begin
    
Select  to_date(to_char(to_date(to_char(Datechar),  ' YYYY-MM-DD HH24:MI:SS ' ),
                           
' YYYY-MM-DD ' ),
                   
' YYYY-MM-DD ' )
    
Into  ReallyDo
    
From  Dual;
    
Return (ReallyDo);
End  CDate;

/*
--功能:将字符串转为年月日时分秒格式.
--来源:http://jorkin.reallydo.com/article.asp?id=529
--参数:
      Datechar Varchar2
--Oracle9i测试通过
*/

Create   Or   Replace   Function  CDateTime(Datechar  In   Varchar2 Return  Date  Is
    ReallyDo Date;
Begin
    
Select  to_date(to_char(to_date(to_char(Datechar),  ' YYYY-MM-DD HH24:MI:SS ' ),
                           
' YYYY-MM-DD HH24:MI:SS ' ),
                   
' YYYY-MM-DD HH24:MI:SS ' )
    
Into  ReallyDo
    
From  Dual;
    
Return (ReallyDo);
End  CDateTime;

/*
--功能:类似MSSQL的日期比较函数
--来源:http://jorkin.reallydo.com/article.asp?id=529
--更新时间:20080721
--参数:
      Datepart  Varchar2 -- 比较年?月?日?
      StartDate Varchar2 -- 起始日期
      EndDate   Varchar2 -- 结束日期
--Oracle9i测试通过
*/

Create   Or   Replace   Function  Datediff
(
    
Datepart    In   Varchar2 ,
    StartDate 
In   Varchar2 ,
    EndDate   
In   Varchar2
Return   Number   Is
    ReallyDo Numeric;
Begin
    
Select   Case   Upper (Datepart )
               
When   ' YYYY '   Then
                Trunc(Extract(
Year   From  CDate(EndDate))  -
                      Extract(
Year   From  CDate(StartDate)))
               
When   ' M '   Then
                
Datediff ( ' YYYY ' , StartDate, EndDate)  *   12   +
                (Extract(
Month   From  CDate(EndDate))  -
                 Extract(
Month   From  CDate(StartDate)))
               
When   ' D '   Then
                Trunc(CDate(EndDate) 
-  CDate(StartDate))
               
When   ' H '   Then
                
Datediff ( ' D ' , StartDate, EndDate)  *   24   +
                (to_Number(to_char(CDateTime(EndDate), 
' HH24 ' ))  -
                 to_Number(to_char(CDateTime(StartDate), 
' HH24 ' )))
               
When   ' N '   Then
                
Datediff ( ' D ' , StartDate, EndDate)  *   24   *   60   +
                (to_Number(to_char(CDateTime(EndDate), 
' MI ' ))  -
                 to_Number(to_char(CDateTime(StartDate), 
' MI ' )))
               
When   ' S '   Then
                
Datediff ( ' D ' , StartDate, EndDate)  *   24   *   60   *   60   +
                (to_Number(to_char(CDateTime(EndDate), 
' SS ' ))  -
                 to_Number(to_char(CDateTime(StartDate), 
' SS ' )))
               
Else
                
- 29252888
           
End
    
Into  ReallyDo
    
From  Dual;
    
Return (ReallyDo);
End  Datediff ;

文章来源: http://Jorkin.Reallydo.Com/default.asp?id=529

转载于:https://www.cnblogs.com/pboy2925/archive/2008/07/03/1248019.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值