Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to subtract an amount of days from a date in SQL?

I know about DATEDIFF(d, date1, date2), but I am not looking to subtract two dates, rather an amount of days from a date.

For example:

"2010-04-13" - 4 = "2010-04-09"

Is that possible with mySQL?

like image 754
Alex Avatar asked Sep 03 '25 04:09

Alex


2 Answers

date_sub(date,interval 4 day);
like image 79
Mickey Avatar answered Sep 04 '25 19:09

Mickey


Yes. See http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_adddate

SELECT DATE_ADD('2008-01-02', 31);

Results in:

'2008-02-02'

To subtract, just use a negative number, or use DATE_SUB

like image 30
Tony the Pony Avatar answered Sep 04 '25 18:09

Tony the Pony