Get week number from dates in T-SQL
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Have a look at DATEPART
SELECT DATEPART(wk, GETDATE())
回答2:
It is best to use the following:
select DATEPART(ISO_WEEK, getDate())
As when you have a year with a week 53 as in the case of 2015 it give unreliable results. (Certainly on 2008 R2)
select DATEPART(WK, '01/03/2016')
Gives variable results around week 53. When run the week after 3rd Jan it produced the value 1. When run now for the same date it gives the value 2.
回答3:
Or use DATEDIFF like this: DATEDIFF(wk,GETDATE(),GETDATE()+7)
to find the number of weeks between two days