How can we utilize the sql date_format function?
The DATE_FORMAT function in SQL is used for formatting dates. Its syntax is as follows:
DATE_FORMAT(date, format)
In this, date is the date or datetime field to be formatted, and format is the string representing the date format. Common date format strings are as follows:
- %Y: A four-digit year
- %y: Two-digit year
- %m: a two-digit month
- %c: Month
- %d: Two-digit date.
- %e: date
- %H: Hours in 24-hour format
- The hour number in a 12-hour clock.
- %i: number of minutes
- %s: seconds
- %p: AM or PM
- %W: Day of the week
- %M: Name of the month
- Abbreviation of month names
- %a: Abbreviation of the weekday name
Here are some examples:
SELECT DATE_FORMAT('2022-01-01', '%Y-%m-%d'); -- 输出 2022-01-01
SELECT DATE_FORMAT('2022-01-01', '%d/%m/%Y'); -- 输出 01/01/2022
SELECT DATE_FORMAT('2022-01-01 12:34:56', '%Y-%m-%d %H:%i:%s'); -- 输出 2022-01-01 12:34:56
SELECT DATE_FORMAT('2022-01-01 12:34:56', '%W, %M %e, %Y'); -- 输出 Saturday, January 1, 2022
By adjusting the value of the format parameter, different date formats can be obtained. The specific date format string can be adjusted as needed.