week-number.net

Programming Languages

Week number in MySQL

MySQL has two functions that return the ISO 8601 calendar week number: weekofyear(date) and week(date[, mode]). The first function returns the ISO 8601 week for a given date, whereas the second one returns the number in many different modes (see documentation for details).

Example: using WEEKOFYEAR()

Select the current week number.


SELECT WEEKOFYEAR(NOW());

Output:

12

--

Example: using WEEK()

Select the current week number using the WEEK() function.


SELECT WEEK(NOW(), 3);

Output:

12

--

Example: Week number of a specific date

To get the calendar week number for a specific date, use the following command:


SELECT WEEK('1984-12-18', 3)

or


SELECT WEEKOFYEAR('1984-12-18')

Output:

51
comments powered by Disqus