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
The DATE_ADD() function adds a time/date interval to a date and then returns the date.
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.
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.
You could use DATE_ADD : (or ADDDATE with INTERVAL
)
UPDATE table SET date = DATE_ADD(date, INTERVAL 1 YEAR)
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()
.
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