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
Related
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)
I'm amending a current query which I run on a fairly regular basis for a membership team looking at recent expiries. The clause in that query is:
and date_expiry between '2019-11-01' and '2019-12-31'
The dates are expanded to cover a 2 month period.
What I'd like to do is to create this query as an excel view in which they can refresh as an when they want.
What I have so far and works to a degree* is the following:
and date_expiry between curdate()- interval 1 month and curdate()+ interval 3 month
However the issue many may have picked up on is that the above query gathers data from today 1 month previous (10/11/2019) and 3 months from today (10/02/2020).
So I've been searching around and the closest I've got was this:
and month(date_expiry) = month(current_date- interval 1 month ) and year(date_expiry)= year(curdate())
This works perfectly for collecting everything in the previous month (01/11/2019-31/11/2019) but I somehow need to add something similar to gather data data for the advanced months.
Help please!
The curdate() suggests MySQL. You can handle full dates as:
where date_expiry >= (curdate() - interval (1 - day(curdate())) day) - interval 1 month and
date_expiry < (curdate() - interval (1 - day(curdate())) day) + interval 1 month
This is convenient because it is index-friendly.
Try DATE_SUB for substraction and DATE_ADD for 2 months advanced
cek this query is this the day you want to cek?
you can change the interval if you want and change the NOW() with your custom date yourself.
to learn about interval check this link
SELECT DATE_SUB(DATE(CONCAT_WS('-', YEAR(NOW()) , MONTH(NOW()), 31)),INTERVAL 1 MONTH) AS lastdaymonthbefore,
DATE_ADD(DATE(NOW()),INTERVAL 2 MONTH) AS 2monthAdvanceFromToday
so you can edit your query like this
AND date_expiry BETWEEN DATE_SUB(DATE(CONCAT_WS('-', YEAR(NOW()) , MONTH(NOW()), 31)),INTERVAL 1 MONTH) AND DATE_ADD(DATE(NOW()),INTERVAL 2 MONTH)
I'm setting up a cleanup on accounts in my database, and only want to run it on accounts that haven't been checked for cleaning in the last 3 months.
Once an account has been checked, regardless if it gets cleaned, I save the date that it's been checked in the following format eg 2019-05-29
When checking for accounts that need to be cleaned, how do I write the
WHERE `accountLastCheckedDate` > 3 months
part of the query please?
Thank you for your time and help.
You want accountLastCheckedDate older than today minus 3 months
SELECT * FROM tablename
WHERE accountLastCheckedDate < DATE_ADD(CURDATE(), INTERVAL -3 MONTH)
or newer?
WHERE accountLastCheckedDate >= DATE_ADD(CURDATE(), INTERVAL -3 MONTH)
This will help you, gl hf
Select * FROM My Table where columname>=DATEADD(m, -3, GETDATE())
If you are interested in date and time:
WHERE `accountLastCheckedDate < `DATE_SUB(NOW(), INTERVAL 3 MONTH);
If you are interested in just the date:
WHERE `accountLastCheckedDate` < DATE_SUB(CURDATE(), INTERVAL 3 MONTH);
Previously I had the query set to grab the last 4 weeks but it didn't count full weeks which threw off Excel pivot tables dependent on the queried data.
I changed it to this and now it's curtailing my data to the last 2 weeks only.
I suspect the AND statement is faulty. Can someone confirm?
WHERE
BalanceDay >= DATE(CONVERT_TZ(CURRENT_TIMESTAMP, 'UTC', Mapping.DEFAULT_TIMEZONE) - INTERVAL DAYOFWEEK(CURRENT_TIMESTAMP) - 1 DAY - INTERVAL 4 WEEK)
AND BalanceDay <= DATE(CONVERT_TZ(CURRENT_TIMESTAMP, 'UTC', Mapping.DEFAULT_TIMEZONE) - INTERVAL DAYOFWEEK(CURRENT_TIMESTAMP) - 1 DAY - INTERVAL 3 WEEK) + INTERVAL 6 DAY
This is what I had previously which did pull 4 weeks but didn't pull in full weeks (i.e. if it ran on a Friday, I would get something other than 28 days' worth of data):
WHERE
BalanceDay >= CONVERT_TZ(CURRENT_DATE, 'UTC', Mapping.DEFAULT_TIMEZONE) - INTERVAL 4 WEEK
Well, I don't understand the real business of your query. but after running it, this query got the last four weeks:
WHERE
BalanceDay >= DATE(CONVERT_TZ(CURRENT_TIMESTAMP, 'UTC', Mapping.DEFAULT_TIMEZONE) - INTERVAL DAYOFWEEK(CURRENT_TIMESTAMP) - 1 DAY - INTERVAL 4 WEEK)
AND DATE(CONVERT_TZ(CURRENT_TIMESTAMP, '+00:00', '+03:00') - INTERVAL DAYOFWEEK(CURRENT_TIMESTAMP) - 1 DAY - INTERVAL 1 WEEK) + INTERVAL 6 DAY
consider using DATE_ADD & DATE_SUB though.
good luck :)
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
;