Skip to content
Advertisement

Exclude weekends and public holiday for SQL datediff

this code works but seems like the results are off by 1 day

Objective: Find the datediff for the two dates given.

However, we will have to exclude

  • Weekends
  • Public Holiday

Example

The difference between 2017-11-04 00:00:00 and 2017-11-22 10:21:00

  • Expected Results: 12.42 (Excluded 3 Saturdays & Sundays)

  • Current Results: 13.42

The code is not written by me, so i’m trying to understand it. I commented some parts that i am unclear as well.

Was thinking to just correct the logic instead of rewriting the entire code. Thanks!

Advertisement

Answer

Like has been mentioned, the best way is to create a Calendar Table and then use the Working Days column, or whatever it is you called it, to calculate the difference.

This is pseudo-SQL, in the absence of a working Calendar Table, but should get you there:

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement