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:
- A unit of time equal to one millionth of a second.
- next
- a very short period of time
- Time of day
- the duration of time from sunrise to sunset
- The period of seven days
- Which month is it?
- 1. A fourth of something
- 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.