Write a PLSQL program to check whether a date falls on weekend i.e. SATURDAY or SUNDAY.
Here is a PLSQL program to check whether a date falls on a weekend (Saturday or Sunday):
DECLARE
input_date DATE;
day_of_week VARCHAR2(10);
BEGIN
-- Ask the user to input a date
input_date := &input_date;
— Use the TO_CHAR function to get the name of the day of the weekday_of_week := TO_CHAR(input_date, ‘DAY’);
— Check if the day of the week is Saturday or Sunday
IF day_of_week = ‘SATURDAY’ OR day_of_week = ‘SUNDAY’ THEN
DBMS_OUTPUT.PUT_LINE(input_date || ‘ falls on a weekend.’);
ELSE
DBMS_OUTPUT.PUT_LINE(input_date || ‘ does not fall on a weekend.’);
END IF;
END;
This program takes an input date in the format “YYYY-MM-DD” and uses the PL/SQL function TO_CHAR to convert the date to a string containing the name of the day of the week. The program then checks if the day of the week is “SATURDAY” or “SUNDAY” and outputs a message accordingly.
You can run this program in SQL Developer by replacing the &input_date with a specific date, for example :
In the above example, the input date is set to "2022-03-13" and the program will output the message "2022-03-13 does not fall on a weekend."
It‘s worth noting that you can also use other built-in functions such as NEXT_DAY() and TO_DATE() to check if a date falls on a weekend as well. This can be useful in cases where you may have a date in a different format or if you want to check if a certain day is a weekend day.
In any case, this program serves as a basic example of how to use PL/SQL to check if a date falls on a weekend, and you can use this as a starting point to build more complex programs that involve date and time operations.