Good morning,
I am on a MySQL database and I have the following query which extracts data from the last 6 months to today’s date :
select distinct
date_format(abc.date,'%d/%m/%Y') as date_bes
from table abc
where abc.date BETWEEN CURDATE() - INTERVAL 6 MONTH AND CURDATE() - INTERVAL 2 WEEK
I want to change my query to the last 6 months, excluding the last current week.
For example I want to exclude week 11 which is from March 13 to March 19
and week 12 which is the current week: from March 20 to 26.
Thanks in advance 🙂
2
Answers
You can do it as follows :
to get the first day of the week starting from sunday :
DATE_ADD(CURDATE(), INTERVAL(1-DAYOFWEEK(CURDATE())) DAY)
to get the first day of the week starting from monday :
DATE_ADD(CURDATE(), INTERVAL(-WEEKDAY(CURDATE())) DAY);
In this case we need to get data till sunday 2023-03-12
You can use WEEKDAY() which:
We use +1 to go back to the Sunday, and another +7 to go back another week.
This solution and the one provided by SelVazi provide slightly different results. I am not sure which you want so here is a table showing the differences: