Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update date + one year in mysql

Tags:

date

mysql

When I want setting numerical value +1 in mysql table, I use e.g.:

UPDATE table SET number=number+1 WHEN ... 

How can I set date + one year?

Thanks

like image 269
Bajlo Avatar asked Oct 05 '10 15:10

Bajlo


People also ask

How do I add 12 months to a date in MySQL?

The DATE_ADD() function adds a time/date interval to a date and then returns the date.

How do you update just the year in SQL?

Solution. After some research I found that we can use the DATEADD() function to select and replace just the year part from the date column to the correct year.

How can I add 15 days to current date in MySQL?

INSERT INTO yourTableName VALUES(DATE_ADD(now(),interval n day)); In the above syntax, you can use curdate() instead of now(). The curdate() will store only date while now() will store both date and time.


2 Answers

You could use DATE_ADD : (or ADDDATE with INTERVAL)

UPDATE table SET date = DATE_ADD(date, INTERVAL 1 YEAR)  
like image 131
Julien Hoarau Avatar answered Sep 21 '22 06:09

Julien Hoarau


This post helped me today, but I had to experiment to do what I needed. Here is what I found.

Should you want to add more complex time periods, for example 1 year and 15 days, you can use

UPDATE tablename SET datefieldname = curdate() + INTERVAL 15 DAY + INTERVAL 1 YEAR; 

I found that using DATE_ADD doesn't allow for adding more than one interval. And there is no YEAR_DAYS interval keyword, though there are others that combine time periods. If you are adding times, use now() rather than curdate().

like image 35
Fred McIntyre Avatar answered Sep 18 '22 06:09

Fred McIntyre