I'm working on a custom column on SQL. We want to see the average datediff but we need to exclude the weekend. In my opinion, subtracting 2 from any value between 7 and 13, subtracting 4 from 14 and 20 etc would be the best way of doing this.
Is there any way to do this?
I figured this would be the query but it's not working.
WHEN TimeToInspect Between 7 and 13 THEN..... not sure how to subtract the two but I figured it would go after that.
Any ideas??
Your method will produce incorrect results. The datediff from Friday to Monday is 3. Since that is less than 7, you wouldn't omit the weekend.
If you want to calculate the number of weekdays between two dates, you need to identify which days are weekdays. The following script will count the number of weekdays between two dates.
The count includes both the begin and end of the requested range. For example, it will return :
You'll need to decide how to adjust this to handle the edge cases.
I'm guessing you'll also want to adjust this to omit other days. I've done this for holidays using the same methodology. My Date table includes
Excluding holidays (or whatever your other omissions may be) would change the calculation: