CREATE FUNCTION dbo.fnGetDatesFromYearAndWeek
(
	@Year SMALLINT,
	@Week TINYINT
)
RETURNS TABLE
AS
RETURN	SELECT	CASE
			WHEN @Year = 1 AND @Week = 1 THEN CAST('00010101' AS DATE)
			WHEN @Year = 9999 AND @Week = 52 THEN CAST('99991227' AS DATE)
			WHEN @Year >= 1900 THEN DATEADD(DAY, 7 * @Week + DATEDIFF(DAY, '18940108', DATEFROMPARTS(@Year, 1, 4)) / 7 * 7, CAST('18940101' AS DATE))
			ELSE DATEADD(YEAR, -2000, DATEADD(DAY, 7 * @Week + DATEDIFF(DAY, '18940108', DATEFROMPARTS(@Year + 2000, 1, 4)) / 7 * 7, CAST('18940101' AS DATE)))
		END AS StartDate,
	CASE
		WHEN @Year = 1 AND @Week = 1 THEN CAST('00010107' AS DATE)
		WHEN @Year = 9999 AND @Week = 52 THEN CAST('99991231' AS DATE)
		WHEN @Year >= 1900 THEN DATEADD(DAY, 7 * @Week + DATEDIFF(DAY, '18940108', DATEFROMPARTS(@Year, 1, 4)) / 7 * 7, CAST('18940107' AS DATE))
		ELSE DATEADD(YEAR, -2000, DATEADD(DAY, 7 * @Week + DATEDIFF(DAY, '18940108', DATEFROMPARTS(@Year + 2000, 1, 4)) / 7 * 7, CAST('18940107' AS DATE)))
	END AS EndDate;