Friday, 26 August 2011

Weekdays, Weekends, and Holidays

Question:
I need to use an anonymous block to populate a table with 30 consecutive dates. The table contains the columns SALE DAY and DAY TYPE, with R indicating regular days, W indicating weekends, and H indicating holidays. How would I do that?
Answer:
Otis, you can use the TO_CHAR function to determine the number or name of the day, and from that whether your day falls on a weekday or weekend. Example:
DECLARE
FUNCTION day_type (date_in IN DATE)
RETURN VARCHAR2
IS
l_daynum PLS_INTEGER := TO_NUMBER (TO_CHAR (date_in, 'D'));
BEGIN
IF l_daynum IN (1, 7)
THEN
RETURN 'Weekend';
ELSE
RETURN 'Weekday';
END IF;
END day_type;
BEGIN
FOR daynum IN 0 .. 29
LOOP
DBMS_OUTPUT.put_line (SYSDATE+daynum);
DBMS_OUTPUT.put_line (' is a ' || day_type (SYSDATE+daynum));
END LOOP;
END;

No comments:

Post a Comment