When using the dateadd/datdiff
trick to round dates I've always had very predictable and understandable results with days and months. I am now using it for the first time with weeks and am experiencing something I was not expecting.
The 19th and 26th of July 2015 seem to get moved into what I consider the next week. The following example illustrates the actual behavior in the second column and the expected behavior in the third column. I'm trying to understand WHY and HOW there is a difference.
declare @n as table ([N] [int] )
insert into @n ( [N] ) values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14),(15)
select n
, dateadd(dd,n,'2015-jul-17')
, dateadd(wk,datediff(wk,0, dateadd(dd,n,'2015-jul-17') ),0)
, dateadd(dd,datediff(dd,0, dateadd(dd,n,'2015-jul-17') )/7*7,0)
from @n
order by n