MySQL add days to a date

MysqlSql

Mysql Problem Overview


I have a table in MySQL. What would be the sql statement look like to add say 2 days to the current date value in the table?

UPDATE classes 
SET 
date = date + 1
where id = 161

this adds one second to the value, i don't want to update the time, i want to add two days?

Mysql Solutions


Solution 1 - Mysql

Assuming your field is a date type (or similar):

SELECT DATE_ADD(`your_field_name`, INTERVAL 2 DAY) 
FROM `table_name`;

With the example you've provided it could look like this:

UPDATE classes 
SET `date` = DATE_ADD(`date` , INTERVAL 2 DAY)
WHERE `id` = 161;

This approach works with datetime , too.

Solution 2 - Mysql

UPDATE table SET nameofdatefield = ADDDATE(nameofdatefield, 2) WHERE ...

Solution 3 - Mysql

This query stands good for fetching the values between current date and its next 3 dates

SELECT * FROM tableName
WHERE columName BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 3 DAY)

This will eventually add extra 3 days of buffer to the current date.

Solution 4 - Mysql

update tablename set coldate=DATE_ADD(coldate, INTERVAL 2 DAY)

Solution 5 - Mysql

For your need:

UPDATE classes 
SET `date` = DATE_ADD(`date`, INTERVAL 2 DAY)
WHERE id = 161

Solution 6 - Mysql

 DATE_ADD(FROM_DATE_HERE, INTERVAL INTERVAL_TIME_HERE DAY) 

will give the Date after adjusting the INTERVAL

eg.

DATE_ADD(NOW(), INTERVAL -1 DAY) for deducting 1 DAY from current Day
DATE_ADD(NOW(), INTERVAL 2 DAY)  for adding 2 Days

You can use like

UPDATE classes WHERE date=(DATE_ADD(date, INTERVAL 1 DAY)) WHERE id=161

Solution 7 - Mysql

You can leave date_add function.

UPDATE `table` 
SET `yourdatefield` = `yourdatefield` + INTERVAL 2 DAY
WHERE ...

Solution 8 - Mysql

SELECT DATE_ADD(CURDATE(), INTERVAL 2 DAY)

Solution 9 - Mysql

SET date = DATE_ADD( fieldname, INTERVAL 2 DAY )

Solution 10 - Mysql

SELECT ADDDATE(d,INTERVAL 1 DAY)
from table

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBeginnerView Question on Stackoverflow
Solution 1 - MysqlBjoernView Answer on Stackoverflow
Solution 2 - MysqlGustav BarkeforsView Answer on Stackoverflow
Solution 3 - MysqlVinit KadkolView Answer on Stackoverflow
Solution 4 - MysqlrahularyansharmaView Answer on Stackoverflow
Solution 5 - MysqlMarcoView Answer on Stackoverflow
Solution 6 - MysqlKrisView Answer on Stackoverflow
Solution 7 - MysqlhovszabolcsView Answer on Stackoverflow
Solution 8 - MysqlValeklosseView Answer on Stackoverflow
Solution 9 - MysqlRomain BruckertView Answer on Stackoverflow
Solution 10 - MysqlSandeep KhotView Answer on Stackoverflow