Home > other >  Find Good Friday via Oracle stored procedure function
Find Good Friday via Oracle stored procedure function

Time:03-15

I have this requirement to determine dates and one of them I need to filter out Good Friday (only) with a parameter of given year

v_temp_year     varchar2(4) :=  to_char(sysdate, 'YYYY');
//the function would be something below that returns date 
getGoodFridayDate(v_temp_year) 

Now I need to formulate a function to be called in each stored procedure and I feel like a total noob about it. Any help would be greatly appreciated. Please don't tag as duplicate I cannot find solutions here with same requirement .

CodePudding user response:

create or replace
function good_friday(p_year number) return date is
    l_day   pls_integer := 0;
    l_month pls_integer := 3;

    l_lunar pls_integer := mod(p_year,19);
    l_cent  pls_integer := p_year / 100;
    l_equi  pls_integer := mod((l_cent - floor(l_cent / 4) - floor((8 * l_cent   13) / 25)   19 * l_lunar   15),30);
    l_full  pls_integer := l_equi - floor(l_equi / 28) * (1 - floor(l_equi / 28) * floor(29 / (l_equi   1)) * floor((21 - l_lunar) / 11));

begin

    l_day   := l_full - mod((p_year   floor(p_year / 4)   l_full   2 - l_cent   floor(l_cent / 4)),7)   28;

    if l_day > 31 then
        l_month := 4;
        l_day   := l_day - 31;
    end if;
    return to_date(p_year||'-'||l_month||'-'||l_day,'YYYY-MM-DD')-2;
end;
/

SQL> select good_friday(2022) from dual;

GOOD_FRID
---------
15-APR-22

SQL> select good_friday(2011) from dual;

GOOD_FRID
---------
22-APR-11
  • Related