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';
Related
I am having problems with date formats, was trying many formatting solutions but non of them was working.
I do have table with dates and I am summing repeating dates:
SELECT
tt.time,
DATE_FORMAT(tt.time, '%x-%v') AS time_label,
SUM(value) AS value
FROM
time_table tt
GROUP BY DATE_FORMAT(tt.time, '%x-%v')
ORDER BY time ASC
As we can see it's formatting end year date as a new year date. w3school %x is saying
Year for the week where Monday is the first day of the week. Used with %V
and for %v
Week where Monday is the first day of the week (01 to 53). Used with %X
The first week of the year 2020 started on Dec 30, 2019. See Week Numbers for 2020.
Therefore 2019-12-30 12:42:53 is being formatted correctly as 2020-01.
I am Looking for a previous month specific date.
i.e. when the report is run on 25th of current month, the start date should always be previous month 25th.
Your expression for start date can simply be..
=DATEADD("m", -1, now())
If the run date is 25th October the start date will be 25th September.
If the run date is 31st March, the start date will be 28th of February, which is as close as you can reasonably get.
Found a solution for this.
Answer: =dateadd("m",-1,dateserial(year(Today),month(Today),25))
The above expression always looks for previous month 25th date, regardless when we run the report. for example if you always want to run on 11th of previous month then change the 25 to 11 on the above expression.
To get the same date of the previous month, just look for the previous month using DateAdd function like:
=DateAdd(DateInterval.Month,-1,Today())
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.
I easily get Week Number using MySql Week function like this WEEK(SYSDATE())
I just wanted to know how to get from and to date using week number in MySQL.
Result required:
Week No From date To date
Week 25 June 18, 2012 June 24, 2012
This works great for this kind of job:
SELECT
ADDDATE(CURDATE(), INTERVAL 1-DAYOFWEEK(CURDATE()) DAY) _From,
ADDDATE(CURDATE(), INTERVAL 7-DAYOFWEEK(CURDATE()) DAY) _To;
Beware that first day of week here is Sunday. It may depends on your country convention ;)
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.