T-SQL: Calculating number of weekdays between two dates

Tonight someone tweeted trying to find a ColdFusion or SQL solution to counting the number of weekdays (excluding Saturdays and Sundays) between two dates.  Most of the solutions online involving looping over the date range and adding to a counter if that iteration of the loop is a weekday.

That’s fine for limited, but probably not if you need to test dates that span many years or need to run the function 1,000s of times.  Anyway, here’s a SQL Server UDF solution.  It’s late, so hopefully the comments in the code suffice.  It should be straightforward to rewrite this in ColdFusion.

[Read more…]