参考网址 https://www.jb51.net/article/29551.htm
复制代码 代码如下:
SELECT [StartDate] FROM [dbo].[udf_Week](2012,2012) WHERE [Week] = 1
上面SQL语句执行结果:
复制代码 代码如下:
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
函数执行Demo:
原创文章,作者:carmelaweatherly,如若转载,请注明出处:https://blog.ytso.com/tech/database/234121.html