I need to get the last month's dates from 1st to current date. Suppose if today's date is March 25th, I need to get the dates from 1st to 25th of february. Suppose if today's date is March 30th, I need to get the dates from 1st to 28/29th Feb, whatever the maximum final date is available. I have searched a lot to get that, but no luck. Can someone please help me how to get this special case done? I am able to do it on another database, but I want to do this on mysql.
Basically what I did for other database is this --> date between date(to_char(date(add_months(DATE(sysdate) ,-1)),'YYYY-MM-01 00:00:00')) and date(add_months(DATE(sysdate) ,-1))
This should do what you want:
WHERE d BETWEEN DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 1 MONTH), '%Y-%m-01')
AND DATE_SUB(CURDATE(), INTERVAL 1 MONTH);
You can use DATE(DATE_SUB(NOW(), INTERVAL 1 MONTH)). This will automatically limit the result to the last day of the month, so if today's date is March 30th, this will return Feb 28th.
Related
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 have this script which is run on the 15th of every month. This script intends to automatically take the last date of the previous month in the format of 'YYYY-MM-DD'. Is there a sql statement to do that?
For example today is 15th January 2017. I will run my script and it will give me a date of 31th December 2016 in the format 2016-12-31.
If it is 15th December 2016, I will get 30th November 2016 in the format 2016-11-30.
Thank you
You can use curdate() - interval 1 month paired with last_day =)
select last_day(curdate() - interval 1 month) as 'last_day_last_month';
I want to select all dates within the last week.
But not by simply counting the current date - 7, as all posts Ive come across suggest.
This is how I actually have it for the SUM now....
SELECT SUM(total) FROM payforms WHERE user_id = 1 GROUP BY WEEK(date)
This gives me a nice total...
But I want to retrieve all individual records within the last week.
So I can use a BETWEEN query....but how do I get it to look in the current week.
Example...
Tuesday I want it to only find values from Sun, Mon, Tue.
On wednesday, I want it to find Sun, Mon, Tue, Wed.
On Saturday it finds the whole previous week. etc
So to be more clear....
I dont want it to find last Monday, on a monday.
On Mondays it should only display Mondays, if you get what I mean.
Can I do this??
Thanks
select * from payforms where yearweek(date) = yearweek(now());
Although I think MySQL weeks start on Sunday
You can make use of the WEEKDAY date function, which returns the day index of the week.
If I understand your question correctly, you could use this query:
SELECT date, SUM(total)
FROM payforms
WHERE date >= CURDATE() - INTERVAL DAYOFWEEK(CURDATE())-1 DAY
GROUP BY date
Please see fiddle here.
This will SUM all totals for every day of the current week, starting on last Sunday. If you want it to start on last Monday you can use this:
WHERE date >= CURDATE() - INTERVAL WEEKDAY(CURDATE()) DAY
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
I am trying to write a PHP script that will process recurring payments every month, quarter, year, etc. This script will run as a nightly Cron job.
I don't want to run into a situation where somebody subscribes, say, on the 15th of a January, and then gets billed again on the 1st of February.
Is that what would happen if I checked the last payment against INTERVAL 1 MONTH? Or would it be the same as INTERVAL 30 DAY, and only process the payment again on the 15th of February, which is what I want?
Accroding to MYSQL
If you add MONTH, YEAR_MONTH, or YEAR and the resulting date has a day
that is larger than the maximum day for the new month, the day is
adjusted to the maximum days in the new month:
mysql> SELECT DATE_ADD('2009-01-30', INTERVAL 1 MONTH);
-> '2009-02-28' Date arithmetic operations require complete dates and do not work with incomplete dates such as '2006-07-00' or
badly malformed dates:
Thus if you use the 1 month built in function you do not have to worry when the last day of the month is. MYSQL does all the work for you.