I need to subtract 1 month and 4 days with mysql, I saw the command
DATE_ADD (NOW (), - 1 MONTH)
perfect for 1 month but for 1 month and 4 days, using 31 days is not valid for every month that some bring 30, 29, 28. I can not add 31 + 4, 30 + 4, etc.
Definition and Usage The DATE_SUB() function subtracts a time/date interval from a date and then returns the date.
Following the answer from Philip Rego, you can use SELECT GETDATE() - 1 to subtract days from a date.
The DATE_ADD() function adds a time/date interval to a date and then returns the date.
using DATE_SUB
[docs]
like :
DATE_SUB((DATE_SUB(curdate(), INTERVAL 1 MONTH)), INTERVAL 4 DAY)
SELECT DATE_ADD(DATE_ADD(NOW(),INTERVAL -1 MONTH), INTERVAL -4 DAY)
Keep it simple:
SELECT CURDATE() - INTERVAL 1 MONTH - INTERVAL 4 DAY;
or
SELECT '2014-03-27' - INTERVAL 1 MONTH - INTERVAL 4 DAY;
or if you like to preserve the current time:
SELECT NOW() - INTERVAL 1 MONTH - INTERVAL 4 DAY;
(Tested on MySQL 5.1.73 and newer)
SELECT CURRENT_TIMESTAMP + INTERVAL - 1 MONTH + INTERVAL - 4 DAY;
orSELECT CURRENT_DATE + INTERVAL - 1 MONTH + INTERVAL - 4 DAY;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With