Get week number from dates in T-SQL

2019-04-03 08:25发布

问题:

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



标签: tsql