English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, you will learn how to use date and time in SQL.
In addition to strings and numbers, you often need to store dates and/or time values, such as users' birth dates, employees' hire dates, dates of future events, and dates and times of creating or modifying specific rows in the table, etc.
This type of data is called temporary data, and each database engine has a default storage format and data type for storing them. The following table shows the data types supported by the MySQL database server for processing date and time.
Type | Predefined format | Allowed values |
---|---|---|
DATE | YYYY-MM-DD | 1000-01-01 - |
TIME | HH:MM:SS or HHH:MM:SS | -838:59:59 - |
DATETIME | YYYY-MM-DD HH:MM:SS | 1000-01-01 00:00:00 - |
TIMESTAMP | YYYY-MM-DD HH:MM:SS | 1970-01-01 00:00:00 - |
YEAR | YYYY | 1901 - |
The format of the DATE value is YYYY-MM-DD, where YYYY represents the year (4digits), while MM and DD represent the month and day parts of the date (two leading zeros). The time value is usually in HH:MM:SS format, where HH, MM, and SS represent the hour, minute, and second parts of the time, respectively.
The following statement demonstrates how to insert date values into a database table:
INSERT INTO employees (emp_name, hire_date, salary) VALUES ('Adam Smith', '2015-06-24', 4500);
Note:In MySQL, the hour part of the time value may be larger because MySQL treats them as runtime. This means that the time data type can not only be used to represent a time of the day (which must be less than24hours), and can also be used to represent the time interval between two events, which may be greater than24hours, even negative values.
When using the database of a large application, it is usually necessary to store the record creation time or the last modification time in the database, for example, the date and time of user registration or the last time the user updated the password.
In MySQL, you can use the NOW() function to insert the current timestamp, as shown below:
-- Syntax of MySQL database INSERT INTO users (name, birth_date, created_at) VALUES ('Bilbo Baggins', '1998-04-16', NOW());
However, if you do not want to manually insert the current date and time, you can simply use the automatic initialization and automatic update properties of the TIMESTAMP and DATETIME data types.
To assign automatic properties, specify the DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP clauses in the column definition as follows:
-- Syntax of MySQL database CREATE TABLE users ( id INT(4) NOT NULL PRIMARY KEY AUTO_INCREMENT, name VARCHAR(50) NOT NULL UNIQUE, birth_date DATE NOT NULL, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP );
Note:The automatic initialization and update of DATETIME data types are only available in MySQL 5.6.5or higher versions available. If you are using an older version, please use TIMESTAMP.
In some cases, you may only want to get a part of the date or time. In MySQL, you can use specially designed functions for extracting parts of time values, such as YEAR(), MONTH(), DAYOFMONTH(), MONTHNAME(), DAYNAME(), HOUR(), MINUTE(), SECOND(), etc.
The following SQL statement extracts the year part of the birth_date column value, for example, if any user's birthday is1987-01-14then YEAR(birth_date) will return1987.
mysql> SELECT name, YEAR(birth_date) FROM users;
Similarly, you can use the DAYOFMONTH() function to get a specific day of the month, for example, if any user's birth_date is1986-10-06If so, DAYOFMONTH(birth_date) will return6.
mysql> SELECT name, DAYOFMONTH(birth_date) FROM users;
If you want to use a more descriptive and readable date format in the result set, you can reformat the existing date and time values with the DATE_FORMAT() and TIME_FORMAT() functions.
The following SQL statement will set the format in a more readable format.usersin the tablebirth_dateThe format of the column value, for example1987Year1Month14From day to1987Year1Month14The value of the day.
mysql> SELECT name, DATE_FORMAT(birth_date, '%M %e, %Y') FROM users;