EXTRACT()
Anonymous contributor
Published Sep 23, 2024
Contribute to Docs
The EXTRACT()
function in SQL is used to extract a specific part from a given date. It is supported in SQL databases like MySQL and PostgreSQL.
Syntax
EXTRACT(part FROM date)
part
: The part of the date to be extracted. It can beYEAR
,MONTH
, or anything else.date
: The date from which a specific part is to be extracted.
Example
The following example shows how to extract different parts from a given date using the EXTRACT()
function:
SELECT-- Extract the year from the dateEXTRACT(YEAR FROM '2024-09-22 15:30:45') AS YearPart,-- Extract the month from the dateEXTRACT(MONTH FROM '2024-09-22 15:30:45') AS MonthPart,-- Extract the day of the month from the dateEXTRACT(DAY FROM '2024-09-22 15:30:45') AS DayPart,-- Extract the hour from the dateEXTRACT(HOUR FROM '2024-09-22 15:30:45') AS HourPart,-- Extract the minute from the dateEXTRACT(MINUTE FROM '2024-09-22 15:30:45') AS MinutePart,-- Extract the second from the dateEXTRACT(SECOND FROM '2024-09-22 15:30:45') AS SecondPart;
The output for the above code is:
YearPart | MonthPart | DayPart | HourPart | MinutePart | SecondPart |
---|---|---|---|---|---|
2024 | 9 | 22 | 15 | 30 | 45 |
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.