This is similar to the question found here...
Count work days between two dates
The big difference is that I need to calculate my range in hours AND I need to account for partial days, some of which might be weekends. For example, most of the code samples I've see take Saturday and Sunday as a whole, like this which returns the total number of non-weekend days in the range...
SELECT @WeekDays =
(DATEDIFF(dd, @StartDate, @EndDate) + 1)
-(DATEDIFF(wk, @StartDate, @EndDate) * 2)
-(CASE WHEN DATENAME(dw, @StartDate) = 'Sunday' THEN 1 ELSE 0 END)
-(CASE WHEN DATENAME(dw, @EndDate) = 'Saturday' THEN 1 ELSE 0 END)
I need something like this, but in terms of hours. Most forums/solutions would have me multiply the weekend day count by 24, which doesn't work if I enter something like "Saturday at noon" to "Monday at 6pm". I'd be subtracting 48 hours from my total hour count instead of 36.
For the record, here's how I'm doing it now to get ALL hours found in the range...
SELECT @HourCount = DATEDIFF(hh, @StartDate, @EndDate)
I'm open to either calculating it correctly in one move or using @HourCount above and subtracting a "weekend hour count" found in the range. Just not sure how to do either one accurately.
Last note, the all important efficiency thing. Technically, I could probably use a while loop and iterate from @StartDate to @EndDate while incrementing the hours (or even minutes), checking the current day, and adding 1 to a total if it's not a weekend day. This is probably my worst case scenario, but I'm looking for a solution wayyyy more efficient than this. Any suggestions or solutions are welcome.