sql获取该周的开始结束日期
mssql函数
IF EXISTS ( SELECT 1 FROM sysobjects WHERE name = 'fn_GetWeekDate')
DROP FUNCTION fn_GetWeekDate
GO
CREATE function [dbo].fn_GetWeekDate(
@Year int, --年份
@Week int --第几周
)
RETURNS @Table TABLE(beginTime datetime,endTime datetime)
AS
BEGIN
declare @curYearStart datetime
set @curYearStart = CONVERT(datetime,CONVERT(varchar(4),@Year) + '-01-01')
declare @tmpdate datetime
select @tmpdate=dateadd(week,@Week-1,@curYearStart)
insert into @TABLE(beginTime,endTime)
select
case when dateadd(day,-datepart(weekday,@tmpdate)+1,@tmpdate) < @curYearStart then @curYearStart else dateadd(day,-datepart(weekday,@tmpdate)+1,@tmpdate) end,
dateadd(day,7-datepart(weekday,@tmpdate),@tmpdate)
return
END
go
使用
select * from fn_GetWeekDate(2016,2)
结果
beginTime endTime
----------------------- -----------------------
2016-01-03 00:00:00.000 2016-01-09 00:00:00.000