How to select data from last monday to sunday. Like this
`WHERE
`order`.order_createdAt >= date_sub(date_sub(curdate(), interval day(curdate()) - 1 day), interval 1 month)
and `order`.order_createdAt < date_sub(curdate(), interval day(curdate()) - 1 day)`
this show data from last month
Upd. find this
`WHERE WEEK (order_createdAt) = WEEK( current_date)-1
AND YEAR( order_createdAt) = YEAR( current_date );`
But it takes from past sunday to saturday
If you want to check for Last week Monday to This Sunday, which is say Today's date is '2017-01-27' and Last week Monday date will be 2017-01-16 and This Sunday will be 2017-01-22, then you can follow below query,
WHERE
`order`.order_createdAt BETWEEN subdate(curdate(),dayofweek(curdate())+5)
and subdate(curdate(),dayofweek(curdate())-1)`
Hope this would help you out.
Following SQL code might be useful to Presto users who might be searching for same information in reference to same question asked, for data between Last Monday to Next Sunday (an ISO week): -
WHERE date_column_ref BETWEEN date_add('day', dow(localtimestamp) * -1 + 1, localtimestamp) and date_add('day', 7 - dow(localtimestamp), localtimestamp)
select subdate(curdate(), WEEKDAY(curdate()) + 7); # Monday
select subdate(curdate(), WEEKDAY(curdate()) + 1); # Sunday
I been trying to search for the same issue on getting MONDAY to SUNDAY, from a specific day.
I have come-up with the following and hope this helps anyone who is looking for the same solution as I am.
The only issue I have is, I think this can be improved and open for suggestions as it's long.
SELECT
DATE_ADD(DATE('2021-05-30 02:12:43'),
INTERVAL - WEEKDAY(DATE('2021-05-30 02:12:43')) DAY) AS MONDAY,
DATE_ADD(DATE_ADD(DATE('2021-05-30 02:12:43'),
INTERVAL - WEEKDAY(DATE('2021-05-30 02:12:43')) DAY),
INTERVAL 6 DAY) AS SUNDAY
;
Related
How to get mysql record older than 30 days? my code will get all the records even which are inserted two months ago .
WHERE date < DATE_SUB(NOW(), INTERVAL 1 MONTH)
I want only one month ago not bigger than one month
Put both start and end date in the filter.
WHERE date >= CURDATE() - INVERVAL 2 MONTH
AND date < CURDATE() - INTERVAL 1 MONTH
It's verbose and repetitive, but that's an affliction of all SQL code.
Calendar months? If you, on May 7th or anytime in May, want to ask for the calendar month of April, it would be this
WHERE date >= LAST_DAY(CURDATE()) + INTERVAL 1 DAY - INTERVAL 2 MONTH
AND date < LAST_DAY(CURDATE()) + INTERVAL 1 DAY - INTERVAL 1 MONTH
LAST_DAY('2021-05-07') gets you '2021-05-31',
+ INTERVAL 1 DAY then gets you '2021-06-01', then
- INTERVAL 2 MONTH finally gets you '2021-04-01'
It's easy to read and reason about.
CURDATE() gives today's date in place of the current date and time given by NOW(). Lots of historical reporting doesn't care about time of day, just calendar day. So it might be smart to use CURDATE(), depending on your application.
I'm making an SQL query that pulls projects that started in the last week, ideally I want to be able to run this any day of the week and get the same result whether to run it tuesday or friday..
I think this works, only problem is that it starts counting back from saturday instead of sunday..
HAVING MIN(p.start_date) BETWEEN DATE_SUB(CURDATE(),
INTERVAL (DAYOFWEEK(CURDATE()) + 7) DAY) AND DATE_SUB(CURDATE(),
INTERVAL (DAYOFWEEK(CURDATE())) DAY)
You can use yearweek():
where yearweek(min(p.startdate)) = yearweek(curdate() - interval 7 day)
Wracking my brain trying to figure this out, and I can't seem to find any existing threads that help.
Simply, I'd like to find the first day of the week (as a date) but one year ago, for any given date. Our calendar week starts on Sunday.
Here's a snap of the table I have at my disposal
Any help is greatly appreciated!
Thanks!
See this. How do I get the first day of the week of a date in mysql?
You can get your required result this way:
mydate - INTERVAL 1 YEAR + INTERVAL 1-DAYOFWEEK(mydate - INTERVAL 1 YEAR) DAY
Explanation:
mydate - INTERVAL 1 YEAR
gives you the date a year before mydate.
anyday + INTERVAL 1-DAYOFWEEK(anyday) DAY
gives you the Sunday beginning the week of anyday.
Similarly you can get the first day of the month of anyday like this:
LAST_DAY(anyday) + INTERVAL 1 DAY - INTERVAL 1 MONTH
Some people call this week- and month- truncation.
You can use datesub() to subtract one year from today (curdate()). Get the weekday with weekday(). It returns a number from 0 to 6 where 0 is Monday. Subtract that many days plus one, as your first weekday is Sunday not Monday.
date_sub(date_sub(curdate(),
INTERVAL 1 YEAR),
INTERVAL weekday(date_sub(curdate(),
INTERVAL 1 YEAR)) + 1 DAY)
Here's a function to do it:
CREATE FUNCTION `SUNDAY`(indate date) RETURNS date
NO SQL
BEGIN
declare prevyear date;
set prevyear = indate - interval 1 year;
return prevyear - weekday(prevyear) - interval 1 day;
END
With this query:
date(timestamp_column) >= SUBDATE(curdate(), WEEKDAY(curdate())) -
interval 1 week AND date(timestamp_column) < SUBDATE(curdate(), WEEKDAY(curdate()))
I get the 7 days from the previous week. The one that I am looking is 5 business days of previous week in Mysql (Mo-fr). I would be more than greatful if anyone can help. Thanks
Edit thanks :
date(timestamp_column) > DATE_FORMAT(DATE_SUB(DATE_SUB(current_date, INTERVAL DAYOFWEEK(CURRENT_DATE)+5 day), INTERVAL 1 day), '%Y%m%d')
and
date(timestamp_column) < DATE_FORMAT(DATE_SUB(current_date, INTERVAL DAYOFWEEK(CURRENT_DATE)-1 day), '%Y%m%d')does the trick, if anyone wants the code.
Adding AND DAYOFWEEK(timestamp_column) BETWEEN 2 AND 6 should help.
DAYOFWEEK(date)
Returns the weekday index for date (1 = Sunday, 2 = Monday, …, 7 =
Saturday). These index values correspond to the ODBC standard.
source: https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_dayofweek
Ex. If I enter today's date 2014-06-23 it should show me previous year's last Sunday date i.e. 2013-12-29. Should not use any procedure / sub-queries, it should be only a single query. Im using MySQL. Kindly help.
Try something like this:
select (date(now()) - interval dayofweek(now()) - 1 day) - interval (weekofyear(now()) - 1) * 7 day
This will return last year Sunday of Current Month
SELECT DATE_SUB(DATE(NOW()), INTERVAL DAYOFWEEK(NOW())-1 year) last_sun
Fiddle Demo
This will return Last year last sunday
SELECT (DATE_SUB(DATE(NOW()), INTERVAL DAYOFWEEK(NOW())-1 day))- INTERVAL (WEEKOFYEAR(NOW()) - 1) * 7 DAY AS Sunday
Fiddle Demo