某年第一周开始日期
你想获取某一年份第一周开始的日期,Insus.NET给你的答案,可以参考使用下面这个函数:http://www.cnblogs.com/insus/articles/1622988.html
SELECT [StartDate] FROM [dbo].[udf_Week](2012,2012) WHERE [Week] = 1
上面SQL语句执行结果:
但你还是觉得此方法很复杂,而且只需要日期部分,时间部分无需显示。因此Insus.NET更改并产生如下一个新函数,你把函数附加入你的数据库中,即可在程序中应用它了。
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
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: