In SQL Server 2008 the isoweek can be found with this:
SELECT datepart(iso_week, getdate())
Before SQL Server 2008 there were no built-in f
Wow! Very good topic and solution to avoid using "set datefirst 1". I just want to add something. If like me, you also want to return the year with the ISO week, like "2015-01" as being "Year 2015, Week 01", it might be useful for reporting purpose. Since the year from ISO week can be different from the actual year of the date! Here is how I did in combination to your code.
DECLARE @Date AS DATETIME
SET @Date = '2014-12-31'
SELECT
CAST(CASE WHEN MONTH(@Date) = 1 AND Q.ISOweek > 50 THEN YEAR(@Date) - 1
WHEN MONTH(@Date) = 12 AND Q.ISOweek < 3 THEN YEAR(@Date) + 1
ELSE YEAR(@Date)
END
AS VARCHAR(4))
+ '-'
+ RIGHT('00' + CAST(Q.ISOweek AS NVARCHAR(2)), 2) AS ISOweek
FROM (SELECT (datepart(DY, datediff(d, 0, @Date) / 7 * 7 + 3) + 6) / 7 AS ISOweek) Q
Will return "2015-01".