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

后端 未结 2 1961
栀梦
栀梦 2020-12-07 05:33

I have two date columns and trying to measure days between the two dates excluding weekends. I\'m getting a negative number and need help solving.

Table

<         


        
2条回答
  •  萌比男神i
    2020-12-07 06:10

    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
    

提交回复
热议问题