udf_StartDateOfFirstWeek SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO
CREATE FUNCTION udf_StartDateOfFirstWeek ( @Year INT ) RETURNS DATE AS BEGIN DECLARE @StartDate DATE = CAST((CAST(@Year AS VARCHAR(4)) + '-01-01') AS DATE) RETURN CASE WHEN (DATEPART(DW,@StartDate) > 4) THEN DATEADD(DAY,(8 - DATEPART(DW,@StartDate)) ,@StartDate) ELSE DATEADD(DAY,(-(DATEPART(DW,@StartDate)-1)),@StartDate) END END GO