如果在SQL中一周是一年中的第一周,如何获得上周数?

问题描述 投票:1回答:1

我试图得到上周的数据,这一周是一年中的第一周(例如,当前日期是2017年1月1日)以下查询适用于除一年的第一周之外的所有其他周。但是,以下查询在一年的第一周不返回任何内容。

 where DateDim_Date.YEAR=year(DATEADD(Day, -7, getDate()))
 and DateDim_Date.WEEKNUMBER = datePart(wk,getDate())-1

提前致谢!

sql date dateadd getdate datepart
1个回答
1
投票

用这个:

where DateDim_Date.YEAR=year(DATEADD(MONTH, -1, getDate()))
 and DateDim_Date.WEEKNUMBER = datePart(wk,DATEADD(DAY,-7,GETDATE()))

你想得到(当前日期 - 7天)的周数,到(当前日期的周数)-1

© www.soinside.com 2019 - 2024. All rights reserved.