Question

I need to calculate the number of the week following a given week. This is trivial for most weeks within the year, but not at the end / beginning of the year. For example if I need to know the week following week 52 of 2013.

I would like to use the defaul_week_format 3, i.e. a) the weeks start with Monday (instead of Sunday) b) the numbering within a year starts with 1 (instead of 0) and c) week number 1 is the first week with more than 3 days this year.

My approach to e.g. calculate the week after week 36 2014 is the following:

SELECT WEEK(DATE_ADD(STR_TO_DATE('W36 2014 Tuesday', 'W%V %X %W'), INTERVAL 1 WEEK))

For this example I calculate the date of Tuesday in week 36, add one week, and calculate the week of this day. This would work, but even without adding one week, I have this strange behaviour:

It works for the default_week_format 0:

SET @@local.default_week_format=0;
SELECT STR_TO_DATE('W36 2014 Tuesday', 'W%V %X %W');
-> Tuesday of week 36 is 2014-09-09
SELECT week('2014-09-09');
-> 2014-09-09 is in week 36

So far, so good: Tuesday of week 36 is in week 36.

But this simple experiment does NOT work for default_week_format 3:

SET @@local.default_week_format=3;
SELECT STR_TO_DATE('W36 2014 Tuesday', 'W%V %X %W');
-> Tuesday of week 36 is 2014-09-09
-> This is wrong, because numbering should start with 1 (instead of 0 now)!
-> Tuesday of week 36 should be 2014-09-02
SELECT week('2014-09-09');
-> 2014-09-09 is in week 37

So MySQL tells me that Tuesday of week 36 is in week 37!

My only explanation for this:

  • week() takes default_week_format into account
  • str_to_date() ignores the default_week_format

How can I force MySQL function str_to_date to use the default_week_format?

Or: How else can I calculate the number of the following week? I could also do it in PHP, but I get the weeks' numbers from a MySQL database.

Thanks for any help!

Was it helpful?

Solution

I was facing exactly the same problem. My solution - perhaps not the greatest one, but it works.

As you already mentionned: Most of the cases are trivial, you just need to know the year end / beginning. Therefore, I just store the relevant weeks for the needed number of years in the following table:

CREATE TABLE `NEXT_WEEK_TAB` (
  `CUR_WEEK`  varchar(8) NOT NULL,
  `NEXT_WEEK` varchar(8) NOT NULL,
  PRIMARY KEY (`CUR_WEEK`,`NEXT_WEEK`)
)

You get the following week now with the current algorithm:

following_week = (SELECT next_week 
                  FROM   next_week_tab 
                  WHERE  cur_week = <your_current_week>);
if !following_week then following_week++;

As I said: Probably not the nicest solution, but it's pretty simple - and it works.

OTHER TIPS

I think you must use Week with a mode argument

mysql> SELECT WEEK('2008-02-20',0);
        -> 7
mysql> SELECT WEEK('2008-02-20',1);
        -> 8

take a look here : http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_week

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top