سؤال

I posted a question earlier which was answered properly and led me to find that my YEARWEEK() function was using the default mode of zero and was giving me results I did not want. I read the manual for YEARWEEK, which referenced me to WEEK regarding the modes and it said, if no mode argument is used, the default_week_format value is used. I decided that I want mode 6 so I took the necessary steps to change the variable to 6. However, only the WEEK() function is affected by this change.

I get the following results:

SELECT YEARWEEK(NOW())
  201301
SELECT YEARWEEK(NOW(), 6)
  201302
SELECT WEEK(NOW())
  2
SELECT WEEK(NOW(), 6)
  2

Is this a problem with MySQL or is there something I am doing wrong? I read the manual a few times and it specifically states, in the YEARWEEK description, "The mode argument works exactly like the mode argument to WEEK()". However, it does not specifically state (as it does in the WEEK description), "If the mode argument is omitted, the value of the default_week_format system variable is used".

Shouldn't YEARWEEK() use the variable's value if none is provided or am I wrong in that regard? It seems to me that these two functions should behave the same, especially since one refers to the other for this particular part of its description.

هل كانت مفيدة؟

المحلول

Shouldn't YEARWEEK() use the variable's value if none is provided or am I wrong in that regard?

No.

While I understand the argument, this is documented and therefore expected behavior. From the docs on default_week_format:

The default mode value to use for the WEEK() function.

So this system variable is only used for WEEK(). However, as you noted, both functions allow week_format as a second argument. As such, I would encourage you not to set this at the server level, but in your code for clarity.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top