oracle 文本日期相减,日期相减等于整数的问题

最初由 jackywood 发布

[B]自己以前写的函数, 也许对楼主的问题有帮助

sql server中有同名函数

[php]

create or replace function DATEDIFF(datepart varchar2, startdate date, enddate date) return number is

Result number;

v_year   integer;

v_month  integer;

v_day    integer;

v_hour   integer;

v_minute integer;

v_second integer;

v_datepart varchar2(30);

begin

v_datepart := lower(datepart);

/*

Datepart         Abbreviations

year             yy, y

quarter          qq, q

month            mm, m

day              dd, d

week             wk, w

hour             hh, h

minute           mi, n

second           ss, s

*/

v_year := to_char(enddate,'yyyy')-to_char(startdate,'yyyy');

v_month := to_char(enddate,'mm')-to_char(startdate,'mm');

v_day := trunc(enddate) - trunc(startdate);

v_hour := to_char(enddate,'hh24')-to_char(startdate,'hh24');

v_minute := to_char(enddate,'mi')-to_char(startdate,'mi');

v_second := to_char(enddate,'ss')-to_char(startdate,'ss');

if v_datepart in ('year','yy','y') then

result := v_year;

elsif v_datepart in ('quarter','qq','q') then

result := v_year * 4 + ceil(v_month/3)-1;

elsif v_datepart in ('month','mm','m') then

result := v_year * 12 + v_month;

elsif v_datepart in ('week','wk','w') then

result := trunc(v_day/7);

elsif v_datepart in ('day','dd','d') then

result := v_day;

elsif v_datepart in ('hour','hh') then

result := v_day * 24 + v_hour;

elsif v_datepart in ('minute','mi','n') then

result := ( v_day * 24 + v_hour ) * 60 + v_minute;

elsif v_datepart in ('second','ss','s') then

result := ( ( v_day * 24 + v_hour ) * 60 + v_minute ) * 60 + v_second;

else

RAISE_APPLICATION_ERROR(-20001, ''''||datepart||''' is not a recognized dateadd option.' );

end if;

return(Result);

EXCEPTION

WHEN OTHERS THEN

RAISE ;

end DATEDIFF;

/

------

[/php] [/B]

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值