1、两个日期间的天数
select floor(sysdate - to_date('20020405','yyyymmdd')) from dual;
补充;查找2002-02-28至2002-02-01间除星期一和七的天数
select count(*)
from ( select rownum-1 rnum
from all_objects
where rownum <= to_date('2002-02-28','yyyy-mm-dd') - to_date('2002-
02-01','yyyy-mm-dd')+1
)
where to_char( to_date('2002-02-01','yyyy-mm-dd')+rnum-1, 'D' )
not in ( '1', '7' )
2、月份差
mothsbetween(bigtime,smalltime)的,但发现结果不是很对,如果两个时间的日期不同,他就算出日期差都除以31,结果就一堆小数。
select months_between(to_date('01-31-2008','MM-DD-YYYY'),to_date('12-31-1998','MM-DD-YYYY')) "MONTHS" FROM DUAL;
1
select months_between(to_date('02-01-2008','MM-DD-YYYY'),to_date('12-31-1998','MM-DD-YYYY')) "MONTHS" FROM DUAL;
1.03225806451613
定义函数处理月份差
create or replace function fun_month_num (var_begin_date in varchar2,var_end_date in varchar2)
return number
as
is_begin_date date;
is_end_date date;
is_count number(10);
i number(10);
j number(10);
is_this_date date;
begin
is_begin_date := to_date(var_begin_date,'yyyy-mm-dd');
is_end_date := to_date(var_end_date,'yyyy-mm-dd');
is_this_date := last_day(is_end_date) ;
is_count := 0 ;
loop
exit when is_this_date < last_day(is_begin_date)+1 ;
is_count := is_count+1;
is_this_date := last_day(is_this_date)-32;
end loop;
return is_count;
end;
使用
select fun_month_num('2008-09-30','2008-02-01') from dual;