数据库中有一个表,其中需要用到三个参数:
上次执行时间 End_date (例如:2011-6-29)
周期 unit(正整数)
周期类型 unit_type (年、月、周、日四种之一)现在要写一个函数计算在 Exe_date 下次执行时间, 即 2011-6-29 加上 周期*周期类型 后的日期。以前没写过oracle的函数,不太会,希望高人指点。
上次执行时间 End_date (例如:2011-6-29)
周期 unit(正整数)
周期类型 unit_type (年、月、周、日四种之一)现在要写一个函数计算在 Exe_date 下次执行时间, 即 2011-6-29 加上 周期*周期类型 后的日期。以前没写过oracle的函数,不太会,希望高人指点。
解决方案 »
- java连接orcal出错
- 一张表有几个字段id,name,XX,XX。里面有10000条数据,怎么查出名字相同的人的姓名来?
- Oracle怎么从远程计算机导出到本地
- 怎么下载Oracle Sql Developer?
- CURSOR中的where子句不能获得参数的问题。
- 对系统自动创建的LOB字段的索引进行重建为什么不成功?
- oracle 问题
- 请大侠问数据库会话异常是什么原因?急问!!!
- 表a字段减去表c字段再加表c字段之和,b表需分组,a表也需分组,a与c无主外键关系,需通过b连接
- oracle遇到的问题【ORA-12518:监听程序无法分发客户机连接】
- 查询语句中根据日历计算结果
- 创建表空间的疑惑……
begin
RETURN CASE upper(unit_type) WHEN 'Y' THEN add_months(End_date,unit*12)
WHEN 'M' THEN add_months(End_date,unit)
WHEN 'W' THEN End_date+unit*7
WHEN 'D' THEN End_date+unit
ELSE NULL END;
end com_dt;
create or replace function f_cal_date
(End_Date date,Unit int,Unit_Type char) return date is
begin
--Unit_Type:Y-年 M-月 W-周 D-日
return case upper(Unit_Type)
when 'Y' then add_months(End_Date,Unit*12)
when 'M' then add_months(End_Date,Unit)
when 'W' then End_Date+Unit*7
when 'D' then End_Date+Unit
else NULL
end;
exception
when others then
date := null;
return date;
end f_cal_date;
--建立函数
create or replace function fc_getNextDate(
this_date date,
feet number,
feet_type varchar2
) return date
as
next_date date;
begin
--年
if(feet_type = 'year') then
next_date := add_months(this_date, feet * 12);
end if; --月
if(feet_type = 'month') then
next_date := add_months(this_date, feet);
end if; --周
if(feet_type = 'week') then
next_date := this_date + feet * 7;
end if; --日
if(feet_type = 'day') then
next_date := this_date + feet;
end if; return next_date;
end fc_getNextDate;
--调用示例
SQL> select fc_getNextDate(to_date('2011-6-29','yyyy-mm-dd'),2,'year') from dual;
FC_GETNEXTDATE(TO_DATE('2011-6
------------------------------
2013-06-29
SQL> select fc_getNextDate(to_date('2011-6-29','yyyy-mm-dd'),2,'month') from dual;
FC_GETNEXTDATE(TO_DATE('2011-6
------------------------------
2011-08-29
SQL> select fc_getNextDate(to_date('2011-6-29','yyyy-mm-dd'),2,'week') from dual;
FC_GETNEXTDATE(TO_DATE('2011-6
------------------------------
2011-07-13
SQL> select fc_getNextDate(to_date('2011-6-29','yyyy-mm-dd'),2,'day') from dual;
FC_GETNEXTDATE(TO_DATE('2011-6
------------------------------
2011-07-01