Daniel Hutmacher shows how to calculate the number of weekdays between two dates:
It has to be said, if performance is important (and it should be), you’ll know that user-defined scalar T-SQL functions can kill your query. So you might want to implement the above in the form of a CROSS APPLY instead
Here’s where I advocate for two tables on every instance: a tally table and a date table. The date table should look like a date dimension, including every potentially-interesting piece of information about a date—including if it’s a weekday. It might not perform quite as fast as Daniel’s solution (I’d have to test to know for sure), but it’s definitely easier. If you can’t get a viable date table on your instances for whatever reason, Daniel’s solution does work and does not require any additional objects.
Comments closed