Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In MySQL, how to return the week of the month? [duplicate]

Tags:

mysql

The year is divided into 12 months. We can break down a month in four weeks.

In MySQL, how to return the week of the month? (Example: first week: 2 entries, second week: 5 entries, third week: 3 entries; fourth week: 8 entries)

Using Week and WeekOfYear not get the desired result because the functions return the week number YEAR, not month.

like image 340
Guttemberg Avatar asked Aug 20 '12 23:08

Guttemberg


1 Answers

Use the MySQL built-in function week to find the week number for the year, then subtract the week number for the first day of the month.

SELECT WEEK('2012-02-20') - WEEK('2012-02-01')

there are plenty of options in the documentation to customize the call to fit exactly what you are looking for, but I hope my example gives you an idea of what is available to you.

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

like image 83
Bryan Wolfford Avatar answered Nov 03 '22 00:11

Bryan Wolfford