Last Updated : 23 Jul, 2025
The EXTRACT() function in MySQL is a versatile tool used for retrieving specific components of date Whether we need the year, month, day or even the hour or minute.
This function simplifies date manipulation and makes queries involving date and time data more efficient and easier to understand. In this article, We will learn about the EXTRACT() Function in MySQL by understanding various examples.
EXTRACT() Function in MySQLEXTRACT()
function in MySQL is used to retrieve a specific part of a date or time value from a given DATETIME
, DATE
or TIMESTAMP
data type. Syntax:
EXTRACT(unit FROM date)
Parameter:
This method accepts two parameters which are illustrated below:
Returns:
It returns the desired extracted part from a specified date.
Examples of EXTRACT() Function in MySQL Example 1: Extracting the YearTo extract the year from a date:
SELECT EXTRACT(YEAR FROM '2024-09-10') AS Year;
Output:
2024
Example 2: Extracting the Month
To extract the month from a date:
SELECT EXTRACT(MONTH FROM '2024-09-10') AS Month;
Output:
9
Example 3: Extracting the Day
To extract the day from a date:
SELECT EXTRACT(DAY FROM '2024-09-10') AS Day;
Output:
10
Example 4: Extracting the Hour
If you have a DATETIME
or TIMESTAMP
value, you can extract the hour:
SELECT EXTRACT(HOUR FROM '2024-09-10 15:30:00') AS Hour;
Output:
15
Advantages of the EXTRACT()
Function in MySQL 1. Simplifies Date/Time Manipulation:
EXTRACT()
function provides an easy way to isolate specific parts of a date or time, such as year, month, day, hour, etc., without having to perform complex string manipulation or conversions.EXTRACT()
, queries that deal with dates become more readable and maintainable. EXTRACT()
helps in efficiently filtering or grouping records based on parts of a date or time, such as retrieving records from a particular month, year, or day of the week.EXTRACT()
is compatible with DATETIME
, DATE
, and TIMESTAMP
types, allowing it to be used in various scenarios where different types of date/time data are stored.The EXTRACT()
function in MySQL simplifies the process of retrieving specific parts of a date or time value, making it easier to work with date-related data in queries. It's useful for operations like filtering, grouping, or formatting date values in reports and analyses.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4