📜  date_part mysql (1)

📅  最后修改于: 2023-12-03 15:00:21.175000             🧑  作者: Mango

DATE_PART in MySQL
Introduction

DATE_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.

Syntax

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.
Example

Let's see some examples of using the DATE_PART function:

  1. Extracting the year from a date:
SELECT DATE_PART('YEAR', '2022-01-31');

Output:

2022
  1. Extracting the month from a date:
SELECT DATE_PART('MONTH', '2022-01-31');

Output:

1
  1. Extracting the day from a date:
SELECT DATE_PART('DAY', '2022-01-31');

Output:

31
  1. Extracting the hour from a time:
SELECT DATE_PART('HOUR', '12:45:23');

Output:

12
  1. Extracting the minute from a time:
SELECT DATE_PART('MINUTE', '12:45:23');

Output:

45
  1. Extracting the second from a time:
SELECT DATE_PART('SECOND', '12:45:23');

Output:

23
Conclusion

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.