How is the “extract” function used in MySQL?

In MySQL, the EXTRACT() function is used to extract parts of a date or time value. The basic syntax is as follows:

EXTRACT(unit FROM date)

The unit parameter specifies the portion to be extracted, which can be one of the following values:

  1. A unit of time equal to one millionth of a second.
  2. next
  3. a very short period of time
  4. Time of day
  5. the duration of time from sunrise to sunset
  6. The period of seven days
  7. Which month is it?
  8. 1. A fourth of something
  9. The period of twelve months.

For example, to extract the year from a datetime value, you can use the following query:

SELECT EXTRACT(YEAR FROM '2022-01-01');

This will return in 2022. By using different unit values, you can extract different parts of a datetime value.

广告
Closing in 10 seconds
bannerAds