In SQL Server, I am trying to get the week number (with the year) of a given date, European style, so I'm using DATEPART
with ISO_WEEK
argument:
SELECT CAST(DATEPART(year, myDate) AS VARCHAR) + RIGHT('0' + CAST(DATEPART(ISO_WEEK, myDate) AS VARCHAR), 2);
This works well, except for December 31st of 2018, which falls on the 1st week of 2019, but since I'm using DATEPART
for the year and the week separately, this obviously can't work. Here's an example when myDate is 31-12-2018:
SELECT CAST(DATEPART(year, '31-12-2018') AS VARCHAR) + RIGHT('0' + CAST(DATEPART(ISO_WEEK, '31-12-2018') AS VARCHAR), 2);
The above query returns '201801'.
Is there a way to simply get 201901 for December 31st 2018, without testing explicitly for this date ?