October 23, 2015 at 8:18 am
robert.sterbal 56890 (10/23/2015)
...I still remember when the church moved the calendar by a couple of weeks - https://en.wikipedia.org/wiki/Gregorian_calendar
Jeff - there's someone older than you on ssc π
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
October 23, 2015 at 8:21 am
ChrisM@Work (10/23/2015)
robert.sterbal 56890 (10/23/2015)
...I still remember when the church moved the calendar by a couple of weeks - https://en.wikipedia.org/wiki/Gregorian_calendar
Jeff - there's someone older than you on ssc π
I may or may not be older, but Jeff is definitely wiser and smarter!
412-977-3526 call/text
October 23, 2015 at 8:27 am
Hi
Thanks for this simple and effective code.
Is there a similar easy way to get the year of that week we are looking for? Or the "ISO-year"?
Lets say I put in the date '2014-12-31' and get the ISO-WeekNumber = 1, I would also like to get the what year that weeknumber is for (in this case 2015).
I guess I can try to find this out, but if you guys already have some really efficient code to do this, it would be appreciated.
br
Gert
October 23, 2015 at 8:50 am
robert.sterbal 56890 (10/23/2015)
I like the way your formula skips the leap year problem.I still remember when the church moved the calendar by a couple of weeks - https://en.wikipedia.org/wiki/Gregorian_calendar
That would be the reason why I don't use SQL Server for dates before 1753. π Heh... Y2K was easy after that one. π
--Jeff Moden
Change is inevitable... Change for the better is not.
October 23, 2015 at 9:04 am
I don't believe it's a correct calculation for all situations. It doesn't look at the first day of a week. In Europe that is Monday, in other parts of the world this is Sunday.
For instance Sunday 3 January 2016 for us in Europe is week 53 (of year 2015). In US this week 1.
October 23, 2015 at 9:11 am
marc.corbeel (10/23/2015)
I don't believe it's a correct calculation for all situations. It doesn't look at the first day of a week. In Europe that is Monday, in other parts of the world this is Sunday.For instance Sunday 3 January 2016 for us in Europe is week 53 (of year 2015). In US this week 1.
That's why we have the ISO standards, including an ISO week.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
October 23, 2015 at 9:14 am
Beugen (10/23/2015)
HiThanks for this simple and effective code.
Is there a similar easy way to get the year of that week we are looking for? Or the "ISO-year"?
Lets say I put in the date '2014-12-31' and get the ISO-WeekNumber = 1, I would also like to get the what year that weeknumber is for (in this case 2015).
I guess I can try to find this out, but if you guys already have some really efficient code to do this, it would be appreciated.
br
Gert
The "Thursday of the week" calculation from the article is your friend here. I just added the DATEPART(yy,) to that below.
SELECT [2014-12-28] = DATEPART(yy,DATEDIFF(dd,0,'2014-12-28')/7*7+3)
,[2014-12-29] = DATEPART(yy,DATEDIFF(dd,0,'2014-12-29')/7*7+3)
,[2014-12-30] = DATEPART(yy,DATEDIFF(dd,0,'2014-12-30')/7*7+3)
,[2014-12-31] = DATEPART(yy,DATEDIFF(dd,0,'2014-12-31')/7*7+3)
,[2015-01-01] = DATEPART(yy,DATEDIFF(dd,0,'2015-01-01')/7*7+3)
,[2015-12-31] = DATEPART(yy,DATEDIFF(dd,0,'2015-12-31')/7*7+3)
,[2016-01-01] = DATEPART(yy,DATEDIFF(dd,0,'2016-01-01')/7*7+3)
,[2016-01-02] = DATEPART(yy,DATEDIFF(dd,0,'2016-01-02')/7*7+3)
,[2016-01-03] = DATEPART(yy,DATEDIFF(dd,0,'2016-01-03')/7*7+3)
,[2016-01-04] = DATEPART(yy,DATEDIFF(dd,0,'2016-01-04')/7*7+3)
;
--Jeff Moden
Change is inevitable... Change for the better is not.
October 23, 2015 at 9:16 am
marc.corbeel (10/23/2015)
I don't believe it's a correct calculation for all situations. It doesn't look at the first day of a week. In Europe that is Monday, in other parts of the world this is Sunday. For instance Sunday 3 January 2016 for us in Europe is week 53 (of year 2015). In US this week 1.
Not in ISO. Again, it's part of the reason for this ISO standard. Doesn't matter where you're at in the world, the ISO standard dictates that a week will always start on Monday. The code in the article isn't meant to solve for anything else. It doesn't rely on anything having to do with @@DateFirst, settings, which is part of the beauty of the formula and the necessity of the ISO standard.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 23, 2015 at 9:26 am
removed
October 23, 2015 at 9:27 am
Does this means that Sunday 3 January 2016 is also week 53 in US standards?
October 23, 2015 at 9:42 am
On a related note:
What is the ISO standard for midnight?
2015-03-04 23:59
2015-03-0? 00:00
2015-03-05 00:01
Does ? = 4 or 5
412-977-3526 call/text
October 23, 2015 at 9:55 am
In my opinion, "midnight" doesn't exist in programmers' language.
It equals the beginning of the next day, so my answer is "4"
Correction: I mean "5"
October 23, 2015 at 10:52 am
Thanks for the article...5 stars (especially since you didn't say "performant" anywhere) π
October 23, 2015 at 2:17 pm
marc.corbeel (10/23/2015)
Does this means that Sunday 3 January 2016 is also week 53 in US standards?
No. It means that it's week 53 according to ISO standards.
The US is a bit weird. Some companies, like the one I work for, use Sun-Sat for the week and some use Mon-Sun for the week. I even worked for one company that used Fri-Thu for the week.
I also worked for a company that used Mon-Sun for the week except for the first week and last week, which could be "short weeks" of anything from 1 to 7 days depending on where the first day and last day of the year fell.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 24, 2015 at 3:36 pm
robert.sterbal 56890 (10/23/2015)
On a related note:What is the ISO standard for midnight?
2015-03-04 23:59
2015-03-0? 00:00
2015-03-05 00:01
Does ? = 4 or 5
To the best of my knowledge, I know of no temporal system that considers midnight to be at the end of the day. It always marks the beginning of a day. For example, there is no 24:00 for time even in the military.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 15 posts - 46 through 60 (of 79 total)
You must be logged in to reply to this topic. Login to reply