Working with Dates and Times in MySQL

From Techotopia
Revision as of 15:01, 23 October 2007 by Neil (Talk | contribs) (New page: In this chapter of MySQL Essentials we are going to look at storing dates and times in a database table and also retrieving and manipulating these values. == Date and Time Formats == ...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

In this chapter of MySQL Essentials we are going to look at storing dates and times in a database table and also retrieving and manipulating these values.


Contents


Date and Time Formats

MySQL supports a number of date and time column formats. These can be summarized as follows:

  • DATE - Stores a date value in the form YYYY-MM-DD. For example 2008-10-23.
  • DATETIME - Stores a date and time value of the form YYYY-MM-DD HH:MM:SS. For example 2008-10-23 10:37:22. The supported range of dates and times is 1000-01-01 00:00:00 all the way through to 9999-12-31 23:59:59
  • TIMESTAMP - Similar to DATETIME with some differences depending on the version of MySQL and the mode in which the server is running.

Creating Date and Time Fields

A table containing DATE and DATETIME columns is created much the same way as any other columns in a table. For example, we can create a new table called orders which contains order number, order item, order date and order delivery columns as follows:

CREATE TABLE `MySampleDB`.`orders` (
  `order_no` INT  NOT NULL AUTO_INCREMENT,
  `order_item` TEXT  NOT NULL,
  `order_date` DATETIME  NOT NULL,
  `order_delivery` DATE  NOT NULL
)
ENGINE = InnoDB;

The order_date column is a DATETIME field because we want to record the precise time and date that the order was placed. For the delivery date it will be impossible to predict the exact time of day the package will arrive, so we just want to record the date.


Date and Time Formats

Whilst it is most common to store dates using a dash (-) as the delimiter and a colon (:) as the time delimiter it is in fact possible to use any character, or no character between the date and time segments. For example, the following formats all achieve the same result:

2008-10-23 10:37:22
20081023103722
2008/10/23 10.37.22
2008*10*23*10*37*22

As you can see MySQL provides considerable flexibility in how dates and times are formatted.

Date and Time Functions

In addition to providing mechanisms for storing dates and times, MySQL also provides a wide range of functions that can be used to manipulate dates and times. The following table provides a list of the more common functions available for working with times and dates in MySQL:

<colgroup> <col> <col> </colgroup> <thead></thead> <tbody> </tbody>
Name Description
<a href="date-and-time-functions.html#function_adddate">ADDDATE()</a>(v4.1.1) Add dates
<a href="date-and-time-functions.html#function_addtime">ADDTIME()</a>(v4.1.1) Add time
<a href="date-and-time-functions.html#function_convert-tz">CONVERT_TZ()</a>(v4.1.3) Convert from one timezone to another
<a href="date-and-time-functions.html#function_curdate">CURDATE()</a> Return the current date
<a href="date-and-time-functions.html#function_current-date">CURRENT_DATE(), CURRENT_DATE</a> Synonyms for CURDATE()
<a href="date-and-time-functions.html#function_current-time">CURRENT_TIME(), CURRENT_TIME</a> Synonyms for CURTIME()
<a href="date-and-time-functions.html#function_current-timestamp">CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP</a> Synonyms for NOW()
<a href="date-and-time-functions.html#function_curtime">CURTIME()</a> Return the current time
<a href="date-and-time-functions.html#function_date-add">DATE_ADD()</a> Add two dates
<a href="date-and-time-functions.html#function_date-format">DATE_FORMAT()</a> Format date as specified
<a href="date-and-time-functions.html#function_date-sub">DATE_SUB()</a> Subtract two dates
<a href="date-and-time-functions.html#function_date">DATE()</a>(v4.1.1) Extract the date part of a date or datetime expression
<a href="date-and-time-functions.html#function_datediff">DATEDIFF()</a>(v4.1.1) Subtract two dates
<a href="date-and-time-functions.html#function_day">DAY()</a>(v4.1.1) Synonym for DAYOFMONTH()
<a href="date-and-time-functions.html#function_dayname">DAYNAME()</a>(v4.1.21) Return the name of the weekday
<a href="date-and-time-functions.html#function_dayofmonth">DAYOFMONTH()</a> Return the day of the month (1-31)
<a href="date-and-time-functions.html#function_dayofweek">DAYOFWEEK()</a> Return the weekday index of the argument
<a href="date-and-time-functions.html#function_dayofyear">DAYOFYEAR()</a> Return the day of the year (1-366)
<a href="date-and-time-functions.html#function_extract">EXTRACT</a> Extract part of a date
<a href="date-and-time-functions.html#function_from-days">FROM_DAYS()</a> Convert a day number to a date
<a href="date-and-time-functions.html#function_from-unixtime">FROM_UNIXTIME()</a> Format date as a UNIX timestamp
<a href="date-and-time-functions.html#function_get-format">GET_FORMAT()</a>(v4.1.1) Return a date format string
<a href="date-and-time-functions.html#function_hour">HOUR()</a> Extract the hour
<a href="date-and-time-functions.html#function_last-day">LAST_DAY</a>(v4.1.1) Return the last day of the month for the argument
<a href="date-and-time-functions.html#function_localtime">LOCALTIME(), LOCALTIME</a> Synonym for NOW()
<a href="date-and-time-functions.html#function_localtimestamp">LOCALTIMESTAMP, LOCALTIMESTAMP()</a>(v4.0.6) Synonym for NOW()
<a href="date-and-time-functions.html#function_makedate">MAKEDATE()</a>(v4.1.1) Create a date from the year and day of year
<a href="date-and-time-functions.html#function_maketime">MAKETIME</a>(v4.1.1) MAKETIME()
<a href="date-and-time-functions.html#function_microsecond">MICROSECOND()</a>(v4.1.1) Return the microseconds from argument
<a href="date-and-time-functions.html#function_minute">MINUTE()</a> Return the minute from the argument
<a href="date-and-time-functions.html#function_month">MONTH()</a> Return the month from the date passed
<a href="date-and-time-functions.html#function_monthname">MONTHNAME()</a>(v4.1.21) Return the name of the month
<a href="date-and-time-functions.html#function_now">NOW()</a> Return the current date and time
<a href="date-and-time-functions.html#function_period-add">PERIOD_ADD()</a> Add a period to a year-month
<a href="date-and-time-functions.html#function_period-diff">PERIOD_DIFF()</a> Return the number of months between periods
<a href="date-and-time-functions.html#function_quarter">QUARTER()</a> Return the quarter from a date argument
<a href="date-and-time-functions.html#function_sec-to-time">SEC_TO_TIME()</a> Converts seconds to 'HH:MM:SS' format
<a href="date-and-time-functions.html#function_second">SECOND()</a> Return the second (0-59)
<a href="date-and-time-functions.html#function_str-to-date">STR_TO_DATE()</a>(v4.1.1) Convert a string to a date
<a href="date-and-time-functions.html#function_subdate">SUBDATE()</a> When invoked with three arguments a synonym for DATE_SUB()
<a href="date-and-time-functions.html#function_subtime">SUBTIME()</a>(v4.1.1) Subtract times
<a href="date-and-time-functions.html#function_sysdate">SYSDATE()</a> Return the time at which the function executes
<a href="date-and-time-functions.html#function_time-format">TIME_FORMAT()</a> Format as time
<a href="date-and-time-functions.html#function_time-to-sec">TIME_TO_SEC()</a> Return the argument converted to seconds
<a href="date-and-time-functions.html#function_time">TIME()</a>(v4.1.1) Extract the time portion of the expression passed
<a href="date-and-time-functions.html#function_timediff">TIMEDIFF()</a>(v4.1.1) Subtract time
<a href="date-and-time-functions.html#function_timestamp">TIMESTAMP()</a>(v4.1.1) With a single argument, this function returns the date or datetime expression. With two arguments, the sum of the arguments
<a href="date-and-time-functions.html#function_timestampadd">TIMESTAMPADD()</a>(v5.0.0) Add an interval to a datetime expression
<a href="date-and-time-functions.html#function_timestampdiff">TIMESTAMPDIFF()</a>(v5.0.0) Subtract an interval from a datetime expression
<a href="date-and-time-functions.html#function_to-days">TO_DAYS()</a> Return the date argument converted to days
<a href="date-and-time-functions.html#function_unix-timestamp">UNIX_TIMESTAMP()</a> Return a UNIX timestamp
<a href="date-and-time-functions.html#function_utc-date">UTC_DATE()</a>(v4.1.1) Return the current UTC date
<a href="date-and-time-functions.html#function_utc-time">UTC_TIME()</a>(v4.1.1) Return the current UTC time
<a href="date-and-time-functions.html#function_utc-timestamp">UTC_TIMESTAMP()</a>(v4.1.1) Return the current UTC date and time
<a href="date-and-time-functions.html#function_week">WEEK()</a> Return the week number
<a href="date-and-time-functions.html#function_weekday">WEEKDAY()</a> Return the weekday index
<a href="date-and-time-functions.html#function_weekofyear">WEEKOFYEAR()</a>(v4.1.1) Return the calendar week of the date (1-53)
<a href="date-and-time-functions.html#function_year">YEAR()</a> Return the year
<a href="date-and-time-functions.html#function_yearweek">YEARWEEK()</a> Return the year and week