Is there any better approach than the function code below
// checks whether the IN data falls on a weekend
CREATE OR REPLACE FUNCTION business_days(start_date IN DATE)
RETURN NUMBER IS
busdays NUMBER := 0;
stDate DATE;
BEGIN
stDate := TRUNC(start_date);
/* Adjust for ending date on a saturday */
IF TO_CHAR(enDate,'D') = '7' THEN
busdays := busdays - 1;
END IF;
/* Adjust for starting date on a sunday */
IF TO_CHAR(stDate,'D') = '1' THEN
busdays := busdays - 1;
END IF;
else
busdays := 0;
END IF;
RETURN(busdays);
END;
/
In Oracle, days of the week starts with Monday (which corresponds to 1); therefore if you are to find whether a day falls into weekend you need to compare it with number 6 and 7 (corresponding to Saturday and Sunday) such as:
Furthermore, extracting day (in number format) from date and comparing whether it’s ‘6’ or ‘7’ seems like the only option to understand if it’s a weekday or weekend and that works just fine.