/* Mostly about Oracle APEX, ORDS, PL/SQL and a bit of Linux in between */
Today I would like to show an example to demonstrate how you can identify all dates of occurrences of a single weekday within a period of time in PL/SQL by specifying a weekday.
For a project, a form mask in APEX was needed on which the operator should be able to select one or more weekdays (Monday, Tuesday, etc.) within a period of time and specify a time of day and a coach. The aim was to calculate the dates of all selected weekdays between the start and end date with PL/SQL and to store the data in the database table “appointments”. End users can book a support slot on these days via a separate order mask.
Below you can find the specification and body of the PL/SQL package I have used:
CREATE OR REPLACE PACKAGE booking_dates_pkg AS
/* Loops through all weekdays and calls procedure save_dates
* @param pi_coach specifies the coach name
* @param pi_period_from specifies the start date in format DD.MM.YYYY
* @param pi_weekdays specifies the colon concatinated numbers of the weekdays, e.g. 0:4 = Monday and Friday
* @param pi_period_to specifies the end date in format DD.MM.YYYY
* @param pi_period_time specifies the daytime
*/
PROCEDURE loop_through_weekdays (
pi_coach IN VARCHAR2
, pi_period_from IN DATE
, pi_weekdays IN VARCHAR2
, pi_period_time IN VARCHAR2
, pi_period_to IN DATE
);
/* Identifies each date of a passed weekday within the period
* @param pi_coach specifies the coach name
* @param pi_period_from specifies the start date in format DD.MM.YYYY
* @param pi_weekday specifies the number of the weekday, e.g. 0 = Monday
* @param pi_period_to specifies the end date in format DD.MM.YYYY
* @param pi_period_time specifies the daytime
*/
PROCEDURE save_dates (
pi_coach IN VARCHAR2
, pi_period_from IN DATE
, pi_weekday IN NUMBER
, pi_period_time IN VARCHAR2
, pi_period_to IN DATE
);
END booking_dates_pkg;
/
CREATE OR REPLACE PACKAGE BODY booking_dates_pkg
AS
PROCEDURE loop_through_weekdays (
pi_coach IN VARCHAR2
, pi_period_from IN DATE
, pi_weekdays IN VARCHAR2
, pi_period_time IN VARCHAR2
, pi_period_to IN DATE
)
AS
BEGIN
FOR i IN (SELECT TO_NUMBER(REGEXP_SUBSTR(pi_weekdays, '[^:]+', 1, LEVEL)) AS day
FROM dual
CONNECT BY TO_NUMBER(REGEXP_SUBSTR(pi_weekdays, '[^:]+', 1, LEVEL)) IS NOT NULL)
LOOP
save_dates(
pi_coach => pi_coach
, pi_period_from => pi_period_from
, pi_weekday => i.day
, pi_period_time => pi_period_time
, pi_period_to => pi_period_to
);
END LOOP;
END loop_through_weekdays;
PROCEDURE save_dates (
pi_coach IN VARCHAR2
, pi_period_from IN DATE
, pi_weekday IN NUMBER
, pi_period_time IN VARCHAR2
, pi_period_to IN DATE
)
AS
l_day DATE;
l_weekday_name VARCHAR2(30);
l_period_from_year NUMBER;
l_period_to_year NUMBER;
l_count_days NUMBER;
l_count_holidays NUMBER;
BEGIN
l_period_from_year := EXTRACT(year FROM pi_period_from);
l_period_to_year := EXTRACT(year FROM pi_period_to);
/* count all weekdays of a single weekday (e.g. Monday) within the period
* function next_day() depends of the invoking session's NLS_DATE_LANGUAGE and does not allow the specification of NLS values to make it independent
* workaround: to make function next_day() independent, use trunc(sysdate, 'IW') to get the first day of week (Monday) defined by ISO standard,
* and add number of days pi_weekday to identify the name of the selected day of the week uniquely
*/
l_weekday_name := TO_CHAR(TRUNC(sysdate, 'IW') + pi_weekday, 'Day');
l_count_days := ( ( NEXT_DAY ( pi_period_to - 7, l_weekday_name )
- NEXT_DAY ( pi_period_from - 1, l_weekday_name )
) / 7
) + 1;
FOR rec_days IN 0..l_count_days-1
LOOP
/* identify the weekday (subtract 1 day to include the start date as well) and all following occurrences within the period by calculating weekday+7*rec_days */
l_day := ( NEXT_DAY ( pi_period_from - 1, l_weekday_name ) + 7 * rec_days );
SELECT COUNT(holiday_date) INTO l_count_holidays
FROM TABLE(get_german_holidays(l_period_from_year, l_period_to_year))
WHERE holiday_date = l_day;
IF l_count_holidays = 0 THEN
INSERT INTO appointments(date, booked, coach)
VALUES (TO_DATE(TO_CHAR(l_day, 'DD.MM.YYYY') || ' ' || pi_period_time, 'DD.MM.YYYY HH24:MI'), 0, pi_coach);
END IF;
END LOOP;
END save_dates;
END booking_dates_pkg;
/
Via the database function NEXT_DAY() the date of the weekday name passed in the second parameter will be returned which follows the specified date. As an example, NEXT_DAY(’23-AUG-2020′, ‘SUNDAY’) would return the following Sunday (August 30, 2020), even though 23 August is also a Sunday.
The parameter for determining the returning day of the week depends on the NLS_DATE_LANGUAGE of the calling session, at the same time the function does not allow the free specification of NLS values. For this reason, calling NEXT_DAY(’23-AUG-2020′, ‘SUNDAY’) may result in the ORA error “ORA-01846: not a valid day of the week” if a different language (English, in the example above) is stored for NLS_DATE_LANGUAGE within the session. To use the function independently and get the weekday name in the language of the session, we use TRUNC(sysdate, ‘IW’) as a workaround to get the first day of the week according to ISO standard (Monday). To identify which weekdays have been selected on the website, colon-separated return values (from 0 (= Monday) to 4 (= Friday)) are passed as varchar from the APEX page to the procedure loop_through_weekdays (e.g. ‘0:1:3’ for Monday, Tuesday and Thursday) that are added on top of Monday. Via TO_CHAR(TRUNC(sysdate, ‘IW’) + pi_weekday, ‘Day’) we get the selected weekday in the correct language of the session. After determining the specific date of the weekday, all following occurrences of this weekday can be identified by simple mathematical multiplications using a FOR loop. In order for the start day to be taken into account, we use NEXT_DAY(〈start date〉-1, 〈weekday name〉) to obtain the date of the day preceding the start date and use this for the calculations. Via the calculation NEXT_DAY( pi_period_from – 1, l_weekday_name ) + 7 * rec_days we get all further occurrences of this weekday with each run of the loop.
Dates of public holidays should not be saved in the database. To check whether a date represents a public holiday, the function get_german_holidays() described in the previous blog post is used at this point.
Related posts:
How to Find the Next Business Day and Add or Subtract N Working Days with SQL (blogs.oracle.com)
Make NEXT_DAY() independent of the invoking session’s NLS_DATE_LANGUAGE (community.oracle.com)
Add optional NLS_DATE_LANGUAGE parameter to NEXT_DAY() (community.oracle.com)