Calculates the number of workdays between two dates (excluding the two dates, excluding weekend)
Reference from:
How to Compute business days for a Date Range in SQL or Plsql (Doc ID 1014162.6)
There are two ways of doing this:
The first is to use an SQL statement:
SQL> SELECT ((To_number (TRUNC (' To_date (' 2015-04-22 ', ' yyyy-mm-dd '), ' d ')-TRUNC (to_date (' 2015-04-21 ', ' yyyy-mm-dd ') + 6, ' d ' ))/7 * 5) + 2 MOD (7-to_number (To_char (to_date (' 2015-04-21 ', ' yyyy-mm-dd '), ' D ')), 6) + 3 LEAST (to_nu Mber (To_char (to_date (' 2015-04-22 ', ' yyyy-mm-dd '), ' D '))-2, 5) days 4 from dual; Days----------as above SELECT return: 1,2015-04-22 is Wednesday, 2015-04-21 is Tuesday sql> SELECT ((To_number (TRUNC (to_date (' 2015-04-27 ', ' yyyy-mm-dd '), ' D ')-TRUNC (to_date (' 2015-04-24 ', ' yyyy-mm-dd ') + 6, ' D '))/7 * 5) + MOD (7-to_number (To_char (to _date (' 2015-04-24 ', ' yyyy-mm-dd '), ' d '), 6) + LEAST (To_number (To_char (' to_date ', ' 2015-04-27 '), ' d '))-2, 5) days from dual 2 3 4 5/days----------as above select return: 1,2015-04-27 is Monday, 2015-04-24 is Friday that is: SQL The function is to count only the start time (if the weekday) or the end time (if the weekday) is counted as the working day time.
The second is to use a function:
CREATE OR REPLACE function num_business_days (start_date in date, end_date on date) RETURN number is currdate dat E: = start_date; /* Holds the next date */Theday VARCHAR2 (10); /* Day of the Week for Currdate */countbusiness number: = 0; /* counter for business days */BEGIN/Start date must be earlier than end date */IF end_date-start_date <= 0 then RETURN (0); END IF; LOOP/* Go to the next day */currdate: = To_date (currdate+1); /* Finished if end_date is reached */EXIT when currdate = end_date; /* What's Day of the week is it? */SELECT To_char (currdate, ' fmday ') into the theday from dual; /* Count It only if it is a weekday */if Theday <> ' Saturday ' and theday <> ' Sunday ' then Countbus Iness: = countbusiness + 1; END IF; END LOOP; RETURN (countbusiness); END; /sql> SELECT num_business_days (to_date (' 2015-04-21 ', ' yyyy-mm-dd '), to_date (' 2015-04-22 '),' Yyyy-mm-dd ') from dual; Business days-------------0sql> SELECT num_business_days (to_date (' 2015-04-24 ', ' yyyy-mm-dd '), To_date (' 2015-0 4-27 ', ' Yyyy-mm-dd ')) "business days" from dual; Business Day-------------0 that is to say: The Num_business_days function does not count the start time and end time into the weekday time, even if both are working days.
"Translate from MOS article" calculates the number of workdays between two dates in an Oracle database