I'm building a weekly report using MySQL queries. First I get week number by
SELECT WEEK(CURDATE());
Then I need to display the starting date of the week (Monday for my region). How to do it with MySQL only?
Thanks!
Option 2: Monday as the First Day of the WeekSELECT DATEADD(week, DATEDIFF(week, 0, RegistrationDate - 1), 0) AS Monday; In the expression above, we add the specified number of weeks to the 0 date.
MySQL WEEK() Function The WEEK() function returns the week number for a given date (a number from 0 to 53).
MySQL WEEKDAY() Function The WEEKDAY() function returns the weekday number for a given date. Note: 0 = Monday, 1 = Tuesday, 2 = Wednesday, 3 = Thursday, 4 = Friday, 5 = Saturday, 6 = Sunday.
How to Select rows from a range of dates with MySQL query command. If you need to select rows from a MySQL database' table in a date range, you need to use a command like this: SELECT * FROM table WHERE date_column >= '2014-01-01' AND date_column <= '2015-01-01';
For thhose who need the Monday date of the current week.
If you count Monday as the first day of the week:
SELECT STR_TO_DATE(CONCAT(YEARWEEK(NOW(), 1),'Monday'), '%x%v %W');
If you count Sunday as the first day of the week:
SELECT STR_TO_DATE(CONCAT(YEARWEEK(NOW()),'Monday'), '%X%V %W');
-- current week monday
(SELECT DATE_ADD(CURDATE(), INTERVAL - WEEKDAY(CURDATE()) DAY))
-- current week friday
SELECT DATE_ADD((SELECT DATE_ADD(CURDATE(), INTERVAL - WEEKDAY(CURDATE()) DAY)),INTERVAL 4 DAY);
-- next week monday
SELECT date(curdate() - interval weekday(curdate()) day + interval 1 week);
-- next week friday
SELECT DATE_ADD((SELECT date(curdate() - interval weekday(curdate()) day + interval 1 week)),INTERVAL 4 DAY);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With