Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: select date of current week's monday

Tags:

mysql

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!

like image 621
Oleg Mikhailov Avatar asked May 31 '16 06:05

Oleg Mikhailov


People also ask

How do I get the Monday of the week in SQL?

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.

How do I get the current week in MySQL?

MySQL WEEK() Function The WEEK() function returns the week number for a given date (a number from 0 to 53).

How do I get Monday Tuesday in SQL?

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 do I select a date in MySQL?

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';


2 Answers

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');

like image 79
kintsukuroi Avatar answered Sep 28 '22 09:09

kintsukuroi


-- 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);
like image 38
Thanura Jayasekara Avatar answered Sep 28 '22 10:09

Thanura Jayasekara