• 00
  • :
  • 00
  • :
  • 00

sql如何通过当前日期获取上周,上上周,上上上周的起始日期(周一_周七)

-- 当前时间周的起始日期(以周一为例)
select DATEADD(week,DATEDIFF(week,0,getdate()),0)
-- 上周起始:
select dateadd(week,-1,DATEADD(week,DATEDIFF(week,0,getdate()),0))
-- 上上周起始:
select dateadd(week,-2,DATEADD(week,DATEDIFF(week,0,getdate()),0))
-- 上上上周起始:
select dateadd(week,-3,DATEADD(week,DATEDIFF(week,0,getdate()),0))

周七以此类推

 

DECLARE @Date1 DATETIME,@Date2 DATETIME
SET @Date1 = DATEADD(YEAR,@Year-1900,0) -- 获取指定年份第一天 2019
SET @Date2 = DATEADD(SECOND,-1, DATEADD(YEAR,@Year-1900+1,0)) -- 获取指定年份第一天 2019

  

-- 获得季度数据
SELECT DATEPART(QUARTER,'2019-01-01')

执行结果:

 

posted @ 2019-03-25 15:50  Garson_Zhang  阅读(1544)  评论(0编辑  收藏  举报