欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Oracle 存储过程 游标及计算天数的函数

程序员文章站 2022-03-16 22:56:25
create or replace package PKG_GENERAL isTYPE ROW_CURSOR IS REF CURSOR;–返回指定日期的月份中有多少天function daysInMonth(rq date) return number;–返回指定日期离月底还有多少天function daysLeft(rq date) return number;–返回指定日期离年底还有多少天function daysLeftOfYear(rq date) return number;–...

create or replace package PKG_GENERAL is

TYPE ROW_CURSOR IS REF CURSOR;

–返回指定日期的月份中有多少天
function daysInMonth(rq date) return number;

–返回指定日期离月底还有多少天
function daysLeft(rq date) return number;

–返回指定日期离年底还有多少天
function daysLeftOfYear(rq date) return number;

–返回指定日期当年有多少天
function daysOfYear(rq date) return number;

end PKG_GENERAL;


create or replace package body PKG_GENERAL is

–select extract(day from sysdate) year from dual 返回日期中指定的年月日 如:年2006 月7 日30
function daysInMonth
(rq in date)
return number
as
daysOfMonth number(2);
begin
select (last_day(trunc(rq))-trunc(rq,‘Month’)+1) into daysOfMonth from dual;
return daysOfMonth;
end daysInMonth;

–返回指定日期离月底还有多少天
function daysLeft
(rq in date)
return number
as
dl number(2);
begin
select last_day(trunc(rq))-trunc(rq) into dl from dual;
return dl;
end daysLeft;

–返回指定日期离年底还有多少天
function daysLeftOfYear
(rq date)
return number
as
dys number(3);
tempDate date :=add_months(rq,1);
begin
dys:=daysLeft(rq);
while to_char(tempDate,‘yyyy’)=to_char(rq,‘yyyy’) loop
dys:=dys+daysInMonth(tempDate);
tempDate:=add_months(tempDate,1);
end loop;
return dys;
end daysLeftOfYear;
–返回指定日期当年的天数
function daysOfYear(rq date)
return number
as
dys number(3);
tempDate date :=add_months(trunc(rq,‘yyyy’),1);
begin
dys:=daysLeft(trunc(rq,‘yyyy’));
while to_char(tempDate,‘yyyy’)=to_char(rq,‘yyyy’) loop
dys:=dys+daysInMonth(tempDate);
tempDate:=add_months(tempDate,1);
end loop;
return dys+1;
end daysOfYear;

end PKG_GENERAL;

本文地址:https://blog.csdn.net/xyx_0300/article/details/107673077

相关标签: Oracle