sql-servert-sqldatepart

Why SQL datepart(ww,GETDATE()) returns current week + 1


I am trying to get current week of the calendar. Let's assume today is 2022-03-14 12:00:00.

I am using these 2:

select GETDATE() // returns 2022-03-14 12:00:00
select datepart(ww,GETDATE() //returns 12

But as per calendar 2022-03-14 12:00:00 should be 11st week.

From where is the difference coming from and how I can get basically current week?


Solution

  • GetDatePart(ww, ...) in SQL server is dependent on the set datefirst as documented here. You can use GetDatePart(isoww, getdate()) instead to get ISO Week Number.