To extract a week from a date in MySQL, there are several options, but I was asking if there is a difference between WEEK(date, 0) and DATE_FORMAT(date, '%U') from an implementation point of view.
I mean, the 2 functions return the same results, but in the background, is it the same implementation? Does one call the other? Is there a deprecated one?
I’ve searched the MySQL date and time functions documentation without finding any answer. Thanks for your enlightenment!
Note : depending on what you want on the output, you should have to use one or the other with different parameters (0 to 52 weeks or 0 to 53? starts on Sunday or Monday? First week with at least 3 days?), but I’m asking about this particular case.
As commentors said, week() seems to be a shortcut for the general date_format() function.
Plus as @nos mentioned, date_format() returns a string (due to its general behavior I guess), although week() returns an integer.
I wanted to investigate to verify that theory, and looked into the source code.
The two functions are implemented in the item_timefunc.cc file.
And guess what? They both lead to the calc_week function in sql_time.cc
So,
week(date)is not really a shortcut fordate_format(date, '%v'), but they both call the same function, after computing the parameters from the input.