MS Access get ISO standard week number

Absinthe picture Absinthe · Jan 12, 2016 · Viewed 7.5k times · Source

I'm surprised that I can't find any existing solutions to this online but I just need an SQL function that returns an ISO standard week number (i.e. the start of week 1 is always the first Monday of the year).

None of the DatePart function options consistently return the correct result. I had thought the option "vbFirstFourDays - Start with the first week that has at least four days in the new year." but testing it for today (12th Jan) returns week 3, not week 2 (my expression is DatePart("ww",Now(),2) )

This year ISO week 1 starts on 4th Jan, next Year the 2nd Jan and last year it was the 5th of Jan.

Many thanks

Answer

Gord Thompson picture Gord Thompson · Jan 12, 2016

The DatePart function does indeed calculate the ISO-8601 week number almost* correctly when it uses vbMonday for the firstdayofweek argument and vbFirstFourDays for the firstweekofyear argument, e.g.,

DatePart("ww", Date(), vbMonday, vbFirstFourDays)

or, when used directly in an Access query

DatePart("ww", Date(), 2, 2)

* Note that the bug documented here has apparently never been fixed, so the following Mondays in the 21st century are reported as being in week 53 when according to ISO-8601 they should be in week 1 of the following year:

2003-12-29
2007-12-31
2019-12-30
2031-12-29
2035-12-31
2047-12-30
2059-12-29
2063-12-31
2075-12-30
2087-12-29
2091-12-31