-3

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?

Borislav Stefanov
  • 533
  • 3
  • 15
  • 38
  • 2
    See the long paragraph "iso_week datepart" on [DATEPART](https://learn.microsoft.com/en-us/sql/t-sql/functions/datepart-transact-sql?view=sql-server-ver15), The short version is use: `SELECT datepart(isoww,GETDATE())` – Luuk Mar 14 '22 at 10:09
  • 2
    As the documentation [explains](https://learn.microsoft.com/en-us/sql/t-sql/functions/datepart-transact-sql?view=sql-server-ver15#week-and-weekday-datepart-arguments), _the DATEPART return value depends on the value set by SET DATEFIRST`_. – Zhorov Mar 14 '22 at 10:09

1 Answers1

1

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.

Cetin Basoz
  • 22,495
  • 3
  • 31
  • 39