I'm trying to get data going back three months for monthly reports. I got to the point where i can get all the data. The problem is that it goes back 3 months based on the current date.
Example: If today is 7th of November it will give me the data up until the 7th of August.
I need it to give me the data going back three months but starting from the first of the month.
Example: today is the 7th of November, I'll need the data starting from the 1st of August.
Here is the code I'm using to get the data from three months back:
SELECT * FROM 'closed_wo_journal' WHERE date_time_stamp > CURDATE() - INTERVAL 3 MONTH
The date_trunc function is just what the doctor ordered:
SELECT *
FROM closed_wo_journal
WHERE date_time_stamp > DATE_TRUNC('MONTH', CURDATE() - INTERVAL '3 MONTH')
Here is logic to get the first of the month, three months ago:
SELECT j.*
FROM closed_wo_journal j
WHERE j.date_time_stamp >= ( CURDATE() - INTERVAL (1 - DAY(CURDATE()) DAY) ) - INTERVAL 3 MONTH )
Related
I try select data from MySQL for this / last and next calendar month. There are couple of similar posts but none of them address January vs last or December vs next. I know I could do it in PHP around SQL but maybe someone have nice and clean way to address in in SQL. I tried with MOD () but this brings the problem of years. i.e. previous calendar month to avoid 0 in January
SELECT * FROM tbl_reservations WHERE ( (MONTH(tbl_reservations.start) = MOD((MONTH(NOW()) +11 ), 12)) AND ( YEAR(tbl_reservations.start) = YEAR(NOW()) ) )
Any ideas? Thanks.
I think it is pretty easy. For the last calendar month:
where extract(year_month from r.start) = extract(year_month from now() - interval 1 month)
You would can use similar logic for next month.
The above is not index friendly. The index friendly version is more cumbersome:
where r.start < curdate() - interval (1 - day(curdate())) day and
r.start >= (curdate() - interval (1 - day(curdate())) day) + interval 1 month
This gets the first day of the month by subtracting (1 - day(curdate())) days. Date manipulations and comparisons are then used to get dates for the last month.
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 want to get records from last month's same day by keeping in mind for days which will be skipped and should be included in next day.
e.g. on 31st of march, february 31 does not exist so it should skip the query and also if i want to get records on april 30 it will give the results as required, but then on 1st of may, 31st of march will be skipped.
Currently, I am using
SELECT * FROM registrations WHERE orderdate = DATE_SUB(CURDATE(), INTERVAL 1 month)
How can I tackle this in mysql query?
Sorry if i am not able to communicate my query.
Consider the following logic, which retains a record only if subtracting one month did not result in an earlier day value:
SELECT *
FROM registrations
WHERE
orderdate = DATE_SUB(CURDATE(), INTERVAL 1 month) AND
DAY(orderdate) = DAY(DATE_SUB(CURDATE(), INTERVAL 1 month));
I've got an mysql-db with charges in it, with a datetime. Every month i want to create an invoice with all the charges from last month or earlier. So if it´s may 2nd, 5th or 30th 2012, i still only want the invoices from april 2012 or earlier. I've tried with date_sub, but it just subtracts a month, so it only invoices up to the same day of the previous month. How should i do this?
get * from Ads WHERE AdEnd > ??
Ty!
I always found that if you subtract the day of the month in days from the current date, you'll get the last day of the previous month. For example, on the 15th of the month, subtract 15 days, and you'll end up with the last day of the previous month:
SELECT (DATE_SUB(CURDATE(),INTERVAL DAYOFMONTH(CURDATE()) DAY))
If CURDATE() is 2012-05-05 then the above returns 2012-04-30.
So, to get Ads up to the last day of last month, do something like this:
SELECT *
FROM Ads
WHERE AdEnd <= (DATE_SUB(CURDATE(),INTERVAL DAYOFMONTH(CURDATE()) DAY))
This works if AdEnd is a DATE, but if it's a DATETIME, you'll just want to do less than the first of the month, so you subtract one less day to get the first of the month like this:
SELECT *
FROM Ads
WHERE AdEnd < (DATE_SUB(CURDATE(),INTERVAL DAYOFMONTH(CURDATE()) - 1 DAY))
Try date_diff, for example:
SELECT DATEDIFF('2008-11-30','2008-11-29') AS DiffDate
yields:
1
The website I'm working on now has a dashboard showing data entered during the previous week by various users. I select this data using a simple WHERE clause:
SELECT * FROM table WHERE WEEK(date, 1) = WEEK(CURDATE(), 1) - 1
However, the New Year is coming soon, and when a user tries to view the dashboard on, for example, 3rd or 4th of January, my code is clearly going to give me a wrong result, because the last week's number of 2010 is 52. So, what would be the best way to alter my query to take into account the change of the year. And also, it would be cool to make it possible to select data entered 2, 3, 4,... weeks ago.
How about selecting the WEEK of the day seven days ago?
WHERE WEEK(date, 1) = WEEK(CURDATE() - INTERVAL 1 WEEK, 1)
This way you can select data entered 2,3,4 weeks ago:
WHERE WEEK(date, 1) = WEEK(CURDATE() - INTERVAL 2 WEEK, 1)
Why you can't use:
WHERE date between date_add(CurDate() - INTERVAL 1 WEEK) and CurDate()