Thursday, March 19, 2015

Script for Remove Sunday Between To dates of a month


SELECT (TO_DATE (:TO_DATE) - (TO_DATE (:from_date)) + 1)
- (SELECT COUNT (days)
FROM (SELECT ( TO_DATE (TO_CHAR (TO_DATE (:from_date),'YYYYMMDD'),'YYYYMMDD')
+ LEVEL
- 1
) days
FROM DUAL
CONNECT BY LEVEL <=
TO_NUMBER (TO_CHAR (TO_DATE (:TO_DATE), 'DD')))
WHERE TO_CHAR (days, 'DY') = 'SUN') AS total_working_days
FROM DUAL;



CREATE OR REPLACE Function custom_add_days
   (start_date_in date, days_in number)
   return date
IS
   v_counter number;
   v_new_date date;
   v_day_number number;

BEGIN

   /* This routine will add a specified number of days (ie: days_in) to a date (ie: start_date). */
   /* It will skip all weekend days - Saturdays and Sundays */
   v_counter := 1;
   v_new_date := start_date_in;

   /* Loop to determine how many days to add */
   while v_counter <= days_in
   loop
 
      /* Add a day */
      v_new_date := v_new_date + 1;
      v_day_number := TO_CHAR(v_new_date, 'd');

      /* Increment counter if day falls between Monday to Friday */
      if v_day_number >= 2 and v_day_number <= 6 then
         v_counter := v_counter + 1;
      end if;

   end loop;

   RETURN v_new_date;
 
EXCEPTION
WHEN OTHERS THEN
   raise_application_error(-20001,'An error was encountered - '||SQLCODE||' -ERROR- '||SQLERRM);
END;

No comments:

Post a Comment