Adding Days To Dates In MySQL

Written by David Walsh on Friday, April 25, 2008


One of my customers recently asked me to increment the date field on an event in their database by 14 days because their upcoming event got delayed. I knew the SQL statement would be simple and after doing some quick research, I found the magical SQL:

UPDATE events SET date_starts = DATE_ADD(date_starts,INTERVAL 14 DAY) WHERE event_id = 3;

This works on DATE and DATETIME field types. Note that it’s “14 DAY” and not “14 DAYS“.


Epic Discussion

Commenter Avatar April 25 / #
Catar4x says:

Nice but I use the time() function, what is better ?

David Walsh April 25 / #
david says:

@Catar: I don’t know which one is better, per say, but I know that this worked wonderfully!

Commenter Avatar April 28 / #
Felix says:

it’s good!!!!…works fine and it’s usefull for me. tks!!

Commenter Avatar May 17 / #

Nice! Wouldn’t have occurred to me that MySQL had such a feature. Personally I prefer to do all my data arithmetic in seconds before handing the result over to an SQL query, but this is worth noting.

Commenter Avatar November 02 / #
Laura says:

Your blog is my Quick Bible in the Caos of Days

Commenter Avatar February 10 / #
red says:

hi,

i’m new to sql and i’m using mysql workbench to generate data mart. i need help..i want to generate dates (e.g. 2005-01-01), day of week ( 1 to 7), day of month (1 to 31), day name and month name for the whole of year 2005. the primary key is the date_key which starts from 1 to 365 and i already upload the date key into my database. it just that i need to generate all the above attributes using update/set but i dont know what is the correct sql script to generate them automatically and accordingly. would appreciate if u could help me on this..thanks

Be Heard!

I want to hear what you have to say! Share your comments and questions below.

Name*:
Email*:
Website:  


© David Walsh 2007-2010. Contact David Walsh. Powered by the remarkable MooTools javascript framework.