Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Subtract Days in MySQL

People also ask

How do you minus days from current date in MySQL?

To get yesterday's date, you need to subtract one day from today's date. Use CURDATE() to get today's date. In MySQL, you can subtract any date interval using the DATE_SUB() function. Here, since you need to subtract one day, you use DATE_SUB(CURDATE(), INTERVAL 1 DAY) to get yesterday's date.

How do I subtract days from a timestamp in SQL?

Following the answer from Philip Rego, you can use SELECT GETDATE() - 1 to subtract days from a date.


SELECT DATE(NOW()-INTERVAL 15 DAY)

For a list of units see http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-add


Not entirely related to this question but is related to the title:

SELECT SUBTIME("10:24:21", "5");        -- subtracts 5 seconds. (returns "10:24:16")

SELECT SUBTIME("10:24:21", "01:00:00");  -- subtracts one hour. (returns "09:24:21")

Documentation: MySQL SUBTIME function


Use:

SELECT NOW() - INTERVAL 15 DAY

to keep the datetime precision.