Reference Web site http://www.jb51.net/article/29551.htm
Copy codeThe Code is as follows:
SELECT [StartDate] FROM [dbo]. [udf_Week] (hz2012) WHERE [Week] = 1
Execution result of the preceding SQL statement:
However, you still think this method is complicated and only requires the date part, and the time part does not need to be displayed. Therefore, if Insus. NET changes and generates a new function, you can add the function to your database to apply it in the program.
Copy codeThe Code is as follows:
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 (@ 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
Function execution Demo: