数据库中有一个表,其中需要用到三个参数:
上次执行时间 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的函数,不太会,希望高人指点。
解决方案 »
- ORACLE中将一个表字段赋值到另一个表中
- 一个简单的两个表,update的问题
- 根据ER图写ORACLE SQL 语句
- Oracle 10g 中如何对一个表进行排他锁定 而且需要锁定一定时间
- Oracle9i用OMS模式登录时,报“登录身份证不明确”的错误。请高手帮忙!!!
- oracle一个表有char(1)类型字段,里面记录字符'A','B'.....,请问我想对这个char进行加减运算,得到另外一个字符,如'A'+1='B',请问如何
- 大家来讨论这个简单问题
- oracle8i oracle9ias 是什么意思?
- 请推荐几本入门的数
- UID * 0
- 查询语句中根据日历计算结果
- 创建表空间的疑惑……
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