Oracle: Days between two date and Exclude weekdays how to handle negative numbers

早过忘川 提交于 2019-11-28 02:18:12

Adapted from my answer here:

Get the number of days between the Mondays of both weeks (using TRUNC( datevalue, 'IW' ) as an NLS_LANGUAGE independent method of finding the Monday of the week) then add the day of the week (Monday = 1, Tuesday = 2, etc., to a maximum of 5 to ignore weekends) for the end date and subtract the day of the week for the start date. Like this:

SELECT ( TRUNC( end_date, 'IW' ) - TRUNC( start_date, 'IW' ) ) * 5 / 7
       + LEAST( end_date - TRUNC( end_date, 'IW' ) + 1, 5 )
       - LEAST( start_date - TRUNC( start_date, 'IW' ) + 1, 5 )
          AS WeekDaysDifference
FROM   your_table

With RANGE_TEMP as (

SELECT
    STARTPERIOD start_date,
    ENDPERIOD end_date 
FROM
    TABLE_DATA -- YOUR TABLE WITH ALL DATA DATE

), DATE_TEMP AS (

SELECT
    (start_date + LEVEL) DATE_ALL
FROM
    RANGE_TEMP
CONNECT BY LEVEL <= (end_date - start_date)

), WORK_TMP as (

SELECT
    COUNT(DATE_ALL) WORK_DATE
FROM
    DATE_TEMP
WHERE
    TO_CHAR(DATE_ALL,'D', 'NLS_DATE_LANGUAGE=ENGLISH') NOT IN ('1','7')

), BUSINESS_TMP as (

SELECT
    COUNT(DATE_ALL) BUSINESS_DATE
FROM
    DATE_TEMP
WHERE
    TO_CHAR(DATE_ALL,'D', 'NLS_DATE_LANGUAGE=ENGLISH') IN ('1','7')

) SELECT L.WORK_DATE, H.BUSINESS_DATE FROM BUSINESS_TMP H, WORK_TMP L ;

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!