How to use the DATE_FORMAT function in MySQL to convert dates into different formats
Date is a common data type in databases. In practical applications, we often need to format dates to meet different needs. MySQL provides the DATE_FORMAT function to convert dates into different formats.
The syntax of the DATE_FORMAT function is as follows:
DATE_FORMAT(date, format)
Among them, date is the date to be converted, and format is the specified format string. The following are some commonly used date format codes:
Here are some examples demonstrating how to use the DATE_FORMAT function for date format conversion:
Convert date to year-month-date format:
SELECT DATE_FORMAT('2022-06-30', '%Y-%m-%d');
The output result is: 2022-06-30
Convert date to month -The format of date-year:
SELECT DATE_FORMAT('2022-06-30', '%m-%d-%Y');
The output result is: 06-30-2022
Convert the date to the format of hours:minutes:seconds and use 24 Hour format:
SELECT DATE_FORMAT('2022-06-30 14:30:45', '%H:%i:%s');
The output result is: 14:30:45
Convert the date to the full name of the week:
SELECT DATE_FORMAT('2022-06-30', '%W');
The output result is :Thursday
Convert the date to the abbreviation of the month:
SELECT DATE_FORMAT('2022-06-30', '%b');
The output result is: Jun
The above is the detailed content of How to use the DATE_FORMAT function in MySQL to convert dates to different formats. For more information, please follow other related articles on the PHP Chinese website!