Friday, April 30, 2010

CURDATE() in Oracle and MySQL

There's a big difference between CURDATE in Oracle and MySQL: Although they look similar, in Oracle, the following will work:

CURDATE + 1 or CURDATE -1 wil give you today's date +/- 1 day

However, in MySQL, the following seems to work:

CURDATE() + 1

However, it doesn't actually do a date calculation, it just adds 1 to the integer returned by CURDATE()! So that, if today is 2010-04-30, then

CURDATE() + 1 in MySQL returns 20100431 -- which is a nonsense date!

This can bite you hard if you don't suspect it, since it seems to work just fine most of the time, just not around month boundaries!

Instead, you need to use the ADDDATE(CURDATE(), INTERVAL 1 DAY) syntax in MySQL to get the correct behavior.