January 19, 2015 at 9:46 am
select datediff(wk, '2015-01-11', '2015-01-19') returns 1 (shouldn't it return 2?)
select datediff(wk, '2015-01-10', '2015-01-19') returns 2
Thanks
January 19, 2015 at 9:54 am
I believe the difference in those calculations will use SQL Server's numbered weeks; this will change based on your DATEFIRST settings, but, assuming a DATEFIRST of Monday:
1/11/2015 is one full week before 1/19/2015; you can see the basis for this by running a SELECT DATEPART(WEEK,'1/11/2015'), which should return a week number of 3.
On the other hand, 1/10/2015 is two full weeks before 1/19/2015; again, a SELECT DATEPART(WEEK,'1/10/2015') will show as much, with a week number of 2.
Hence, the difference between today, which is in week number 4, and 1/10/2015's week, which is 2, is 2. For 1/11/2015, the difference is 4 and 3, which is one.
- 😀
January 19, 2015 at 10:12 am
sonchoix (1/19/2015)
select datediff(wk, '2015-01-11', '2015-01-19') returns 1 (shouldn't it return 2?)select datediff(wk, '2015-01-10', '2015-01-19') returns 2
Thanks
As per BoL: "DATEDIFF ... Returns the count (signed integer) of the specified datepart boundaries crossed between the specified startdate and enddate"
Week boundary will depend on DATFIRST setting (default - 7 - Sunday for U.S. English)
So, datediff will count a week for each crossing of week boundary starting from startday (if startday is a boundary day, it's not counted as there is no crossing of 23:59:59 to 00:00:00...)
Your first pair has one crossing, therefore DATEDIFF returns 1,
the second one has two corssings, hence 2 is returned.
January 19, 2015 at 10:56 am
Thank you guys
January 19, 2015 at 11:50 am
Be advised that the "wk" DATEPART can change depending on language settings and DATEFIRST settings. It's not considered to be a good thing to use.
--Jeff Moden
Change is inevitable... Change for the better is not.
January 20, 2015 at 3:33 am
Jeff Moden (1/19/2015)
Be advised that the "wk" DATEPART can change depending on language settings and DATEFIRST settings. It's not considered to be a good thing to use.
+1
I would calculate the difference in days and get the modulo of 7.
(which might be a total different interpretation of difference in weeks)
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply