Sunday, March 16, 2008

Date and Time Functions in MySQL

Quite often I am having to perform date and time calculations in my applications. Earlier I was doing all these things in Ruby. But later I realized these things will be faster and better if performed by the database engine. These calculations become cumbersome as they become complex. Here is a link to MySQL's date and time functions.

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html

2 of the most commonly used functions:
  • Add an interval to a date and get back the updated date.
     DATE_ADD(s.expires_on, INTERVAL p.expiration_buffer DAY)
  • Return date in a particular format. In this case "2008-03-17"
     DATE_FORMAT(subscriptions.expires_on,'%Y-%m-%d')

No comments: