📅  最后修改于: 2023-12-03 15:00:21.175000             🧑  作者: Mango
DATE_PART
in MySQLDATE_PART
is a built-in function in MySQL that allows programmers to extract specific parts of a date or time value. It returns the specified part of a date or time as a number.
The syntax of the DATE_PART
function is as follows:
DATE_PART(part, date)
part
: Specifies the part of the date or time to extract. It can be one of the following values:'YEAR'
: Extracts the year from the date or time.'MONTH'
: Extracts the month from the date or time.'DAY'
: Extracts the day from the date or time.'HOUR'
: Extracts the hour from the date or time.'MINUTE'
: Extracts the minute from the date or time.'SECOND'
: Extracts the second from the date or time.date
: Specifies the date or time value from which to extract the specified part.Let's see some examples of using the DATE_PART
function:
SELECT DATE_PART('YEAR', '2022-01-31');
Output:
2022
SELECT DATE_PART('MONTH', '2022-01-31');
Output:
1
SELECT DATE_PART('DAY', '2022-01-31');
Output:
31
SELECT DATE_PART('HOUR', '12:45:23');
Output:
12
SELECT DATE_PART('MINUTE', '12:45:23');
Output:
45
SELECT DATE_PART('SECOND', '12:45:23');
Output:
23
In conclusion, the DATE_PART
function in MySQL is useful for extracting specific parts of dates or times. It provides flexibility in retrieving desired information from a given date or time value.