SQLServer 获取第几周开始日期

不多说直接上code

DECLARE @CurrDay DATETIME=GETDATE()
--SET @CurrDay=CAST(('2013-01-10')AS DATETIME)
--SET @CurrDay=CAST(('2012-01-10')AS DATETIME)

DECLARE @CurrWK INT=DATEPART(wk,@CurrDay)

DECLARE @YearFisrtDay DATETIME=CAST((DATENAME(yy,@CurrDay)+'-01-01') AS DATETIME)
PRINT @YearFisrtDay
--这一年的第一天是星期几
DECLARE @YearFistWK INT=DATEPART(dw,@YearFisrtDay)
PRINT @YearFistWK

--总多少天 -第一天是属于的周
PRINT DATEADD(DAY,(@CurrWK-1)*7-@YearFistWK+1,@YearFisrtDay)

  

posted @ 2014-05-08 09:40  xu.c  阅读(5337)  评论(1编辑  收藏  举报