In learning how to get the date of a day of the week from the previous week (e.g. the date of Monday from the previous week), I found the following two expressions coming up commonly, where you change the very last number (e.g. 0=Monday) depending on which day you want:
SELECT DATEADD(wk, DATEDIFF(wk, 6, GETDATE()), 0)
SELECT DATEADD(wk, DATEDIFF(wk, 7, GETDATE()), 0)
Both seem to return the correct Date 2017-05-29
when I run it today (2017-06-17).
My question is - how does this work? It seems to be getting a count of weeks since 6
or 7
weeks after 1900-01-01
? And why does it work with both a 6
and 7
?
Also, are there edge cases one would need to be aware of where it might not give the expected results?