Click here to Skip to main content
15,880,392 members
Articles / Programming Languages / SQL

A Simple Math Formula to Get Special Dates

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
8 Oct 2012CPOL2 min read 6.3K   1  
A simple math formula to get special dates.
CREATE OR REPLACE PROCEDURE GETREQUIREDDATE ( 
          inputDate int, basedate date, res OUT date) 
AS 
  add_day      int;
  minus_day    int;
  diff_day     int;
  days         int;
  
BEGIN
    days       :=  to_NCHAR( to_date(baseDate), 'D');    
    add_day    := mod((inputDate - days + 7),7);
    minus_day  := mod((inputDate - days - 7), 7);
    diff_day   := (1- floor(abs(add_day)/3.5)) * add_day + (1-floor(abs(minus_day)/3.5)) * minus_day;
    res        := basedate + diff_day;
    
EXCEPTION
 WHEN OTHERS THEN
       raise_application_error(-20001,'An error was encountered - '||SQLCODE||' -ERROR- '||SQLERRM);

END GETREQUIREDDATE;

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions