wafw1971 (3/1/2013)
Thanks Chris, I have figured it out now:Declare @From Datetime
Declare @To Datetime
Set @From = '07 Jan 2013'
Set @To = '18 Mar 2013'
SELECT Datediff(Week, dateadd(dd, CASE WHEN datepart(weekday, @From) = 1 THEN 0 ELSE 8 - datepart(weekday, @From) END,@From),
dateadd(dd, CASE WHEN datepart(weekday, @To) = 1 THEN 0 ELSE 1 - datepart(weekday, @To) END,@To))+1
Be careful with datepart. From BOL:
"When datepart is week (wk, ww) or weekday (dw), the return value depends on the value that is set by using SET DATEFIRST." The first day of the week is sunday in the US, monday in the UK.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden