A2-04-04.MySQL DATA TYPES-The Ultimate Guide To MySQL DATE and Date Functions

时间:2022-01-09 16:23:12

转载自:http://www.mysqltutorial.org/mysql-date/

The Ultimate Guide To MySQL DATE and Date Functions

 

Summary: in this tutorial, we will introduce you to the MySQL DATE data type and show you some useful date functions to handle the date data effectively.

Introduction to MySQL DATE data type

MySQL DATE is one of  the five temporal data types used for managing date values. MySQL uses yyyy-mm-dd format for storing a date value. This format is fixed and it is not possible to change it.

For example, you may prefer to use mm-dd-yyyy format but you can’t. Instead, you follow the standard date format and use the DATE_FORMAT function to format the date the way you want.

MySQL uses 3 bytes to store a DATE value. The DATE values range from 1000-01-01 to 9999-12-31. If you want to store a date value that is out of this range, you need to use a non-temporal data type like integer e.g., three columns, and each column for the year, month, and day. You also need to create stored functions to simulate the built-in date functions provided by MySQL, which is not recommended.

When strict mode is disabled, MySQL converts any invalid date e.g., 2015-02-30 to the zero date value 0000-00-00.

MySQL Date values with two-digit years

MySQL stores the year of the date value using four digits. In case you use two-digit year values, MySQL still accepts them with the following rules:

  • Year values in the range 00-69 are converted to 2000-2069.
  • Year values in the range 70-99 are converted to 1970 – 1999.

However, a date value with two digits is ambiguous therefore you should avoid using it.

Let’s take a look at the following example.

First, create a table named people with birth date column with DATE data type.

Next, insert a row into the people table.

Then, query the data from the people table.

A2-04-04.MySQL DATA TYPES-The Ultimate Guide To MySQL DATE and Date Functions

After that, use the two-digit year format to insert data into the people table.

In the first row, we used 01 (range 00-69) as the year, so MySQL converted it to 2001. In the second row, we used 80 (range 70-99) as the year, MySQL converted it to 1980.

Finally, we can query data from the people table to check whether data was converted based on the conversion rules.

A2-04-04.MySQL DATA TYPES-The Ultimate Guide To MySQL DATE and Date Functions

MySQL DATE functions

MySQL provides many useful date functions that allow you to manipulate date effectively.

To get the current date and time, you use NOW() function.

 

To get only date part of a DATETIME value, you use the DATE() function.

 

To get the current system date, you use  CURDATE() function as follows:

 

To format a date value, you use  DATE_FORMAT function. The following statement formats the date asmm/dd/yyyy using the date format pattern %m/%d/%Y :

 

To calculate the number of days between two date values, you use the DATEDIFF function as follows:

 

To add a number of days, weeks, months, years, etc., to a date value, you use the DATE_ADD function:

A2-04-04.MySQL DATA TYPES-The Ultimate Guide To MySQL DATE and Date Functions

Similarly, you can subtract an interval from a date using the DATE_SUB function:

A2-04-04.MySQL DATA TYPES-The Ultimate Guide To MySQL DATE and Date Functions

If you want to get the day, month, quarter, and year of a date value, you can use the corresponding function DAYMONTHQUARTER, and YEAR as follows:

 

To get the week information week related functions. For example, WEEK function returns the week number, WEEKDAY function returns the weekday index, and WEEKOFYEAR function returns the calendar week.

 

The week function returns the week number with the zero-based index if you don’t pass the second argument or if you pass 0. If you pass 1, it will return week number with 1-indexed.

 

In this tutorial, you have learned about the MySQL DATE data type and how to use some useful date functions to manipulate date values.