[oracle自定义函数]根据年份查询指定季度的最后一天

通过sql查询指定年份下给定季度的最后一天,如传入2018年的1季度,则返回2018-03-31

这个在oracle中没有特定的函数可以直接实现这个功能

select add_months(trunc(to_date('2018', 'yyyy') + 1, 'q'), 1 * 3) - 1 as resultDay from dual;

把以上的sql封装成oracle函数,以后直接调用就能返回结果,sql如下:

CREATE OR REPLACE FUNCTION "FN_QLASTDAY" (vcyear in varchar2, --年度
                                        vcquarter in varchar2 --季度
                                          ) return date is
  resultDay date;--结果
--根据年份,查询指定季度的最后一天
begin
  select add_months(trunc(to_date(vcyear, 'yyyy') + 1, 'q'), to_number(vcquarter)*3) - 1 into resultDay from dual;
  return resultDay;
end;
调用方法:

--示例:查询2018年1季度的最后一天
select fn_qlastday('2018','1') from dual;
参考链接
https://zhidao.baidu.com/question/560061332.html
http://blog.csdn.net/zutsoft/article/details/44985969

猜你喜欢

转载自blog.csdn.net/u010999809/article/details/79943322