Updated May 12, 2023
Introduction to MySQL EXTRACT() Function
MySQL EXTRACT() function is one of the DATE and DATETIME related function which extracts a portion from the specified Date or DateTime value. This MySQL EXTRACT() function does not implement date calculation but provides the part value of either Date or DateTime value as given in the function arguments. Also, functions like DATE_ADD() and DATE_SUB() having unit identifiers can work together with this EXTRACT () function in MySQL. Using EXTRACT(), the date value can be abstracted from the date, day or month part, etc. Similarly, you can take parts such as seconds, minutes, hours or microseconds, etc., from the DATETIME time section components.
Syntax
We have the following syntax code for MySQL EXTRACT() function:
EXTRACT(Unit_Value FROM DATE_Value)
The terms provided in the syntax as arguments of the EXTRACT() function are described below:
DATE_Value: This represents the required value of Date or DateTime, which is responsible for extracting a date part when the function runs.
Unit_Value: It defines the interval to be fetched as apart from the date value as we want. This can be one of any effective intervals from follows:
- SECOND
- MINUTE
- MICROSECOND
- SECOND_MICROSECOND
- HOUR
- WEEK
- DAY
- MONTH
- YEAR
- QUARTER
- MINUTE_SECOND
- MINUTE_MICROSECOND
- HOUR_SECOND
- HOUR_MICROSECOND
- HOUR_MINUTE
- DAY_SECOND
- DAY_MICROSECOND
- DAY_HOUR
- DAY_MINUTE
- YEAR_MONTH
How does MySQL EXTRACT() Function works?
When we pass on the argument values in the EXTRACT function, then as per the given Date or DateTime value, we can generate a part of it that we want. We also use the MySQL SELECT statement command to perform the EXTRACT() function. Even we can apply the EXTRACT() operation on columns of tables having DATE or DATETIME as a Data Type in the Database.
Suppose we want to abstract the month portion from Date using the following query statement:
SELECT EXTRACT(MONTH FROM '2020-05-17');
The above query will execute the output result as 06 or 6, which denotes the month extracted from the date value set for the function.
Likewise, let us receive the time part from the DateTime section of the function EXTRACT() arguments. The SQL statement is as follows:
SELECT EXTRACT(HOUR FROM '2020-05-17 08:30:25');
So, the function results in 8 to provide the hour part from time value “08:30:25”.
Examples of implementing the EXTRACT() function in MySQL
Let us consider some examples related to MySQL EXTRACT() function and explore the uses of this function in MySQL using different intervals:
1. Abstract Day using MySQL EXTRACT()
Code:
SELECT EXTRACT(DAY FROM '2020-05-10') DAY;
Output:
Explanation: The result of the above query extracted is DAY.
2. Abstract DAY_HOUR using EXTRACT()
Code:
SELECT EXTRACT(DAY_HOUR FROM '2020-05-10 06:20:45') DAYHOUR;
Output:
3. Abstract DAY_MICROSECOND using EXTRACT()
Code:
SELECT EXTRACT(DAY_MICROSECOND FROM '2020-05-10 06:20:45') DayMicrosecond
Output:
4. Abstract DAY_MINUTE using EXTRACT()
Code:
SELECT EXTRACT(DAY_MINUTE FROM '2020-05-10 06:20:45') DAYMINUTE;
Output:
5. Abstract DAY_SECOND using EXTRACT()
Code:
SELECT EXTRACT(DAY_SECOND FROM '2020-05-10 06:20:45') DAYSECOND;
Output:
6. Abstract HOUR using EXTRACT()
Code:
SELECT EXTRACT(HOUR FROM '2020-05-10 06:20:45') HOUR;
Output:
7. Abstract HOUR_MICROSECOND using EXTRACT()
Code:
SELECT EXTRACT(HOUR_MICROSECOND FROM '2020-05-10 06:20:45') HOURMICROSECOND;
Output:
8. Abstract HOUR_MINUTE using EXTRACT()
Code:
SELECT EXTRACT(HOUR_MINUTE FROM '2020-05-10 06:20:45') HOURMINUTE;
Output:
9. Abstract HOUR_SECOND using EXTRACT()
Code:
SELECT EXTRACT(HOUR_SECOND FROM '2020-05-10 06:20:45') HOURSECOND;
Output:
10. Abstract MICROSECOND using EXTRACT()
Code:
SELECT EXTRACT(MICROSECOND FROM '2020-05-10 06:20:45') MICROSECOND;
Output:
11. Abstract MINUTE using EXTRACT()
Code:
SELECT EXTRACT(MINUTE FROM '2020-05-10 06:20:45') MINUTE;
Output:
12. Abstract MINUTE_MICROSECOND using EXTRACT()
Code:
SELECT EXTRACT(MINUTE_MICROSECOND FROM '2020-05-10 06:20:45') MINUTEMICROSECOND;
Output:
13. Abstract MINUTE_SECOND using EXTRACT()
Code:
SELECT EXTRACT(SECOND FROM '2020-05-10 06:20:45') SECOND;
Output:
14. Abstract MONTH using EXTRACT()
Code:
SELECT EXTRACT(MONTH FROM '2020-05-10') MONTH;
Output:
15. Abstract QUARTER using EXTRACT()
Code:
SELECT EXTRACT(QUARTER FROM '2020-05-10 06:20:45') QUARTER;
Output:
16. Abstract SECOND_MICROSECOND using EXTRACT()
Code:
SELECT EXTRACT(SECOND_MICROSECOND FROM '2020-05-10 06:20:45') SECONDMICROSECOND;
Output:
17. Abstract WEEK using EXTRACT()
Code:
SELECT EXTRACT(WEEK FROM '2020-05-10') WEEK;
Output:
18. Abstract YEAR using EXTRACT()
Code:
SELECT EXTRACT(YEAR FROM '2020-05-10 06:20:45') YEAR;
Output:
19. Abstract YEAR_MONTH using EXTRACT()
Code:
SELECT EXTRACT(YEAR_MONTH FROM '2020-05-10 06:20:45') YEARMONTH;
Output:
20. Abstract HOUR_MICROSECOND using EXTRACT()
Code:
SELECT EXTRACT(HOUR_MICROSECOND FROM '2020-05-10 06:20:45.000001') HOURMICROSECOND;
Output:
21. Abstract date part using EXTRACT() with a combination of Unit Specifiers
Code:
SET @date = '2020-05-10 06:20:45';
SELECT EXTRACT(DAY FROM @date) AS Date;
Output:
Explanation: In the above query, we have used a Unit specifier, set the date, and extracted the part from that specifier.
22. Abstract MONTH part using EXTRACT() with Current DATE/TIME function
SQL query to get the date part from the Current date and time value after using the CURDATE() to fetch the current date and time in the query execution:
Code:
SELECTCURDATE(),
EXTRACT(MONTH FROM CURDATE());
Output:
23. Abstract Year and Month part using EXTRACT() with Database Column
SQL query with a Date column present in the database to extract both year and month together with thefunctionEXRACT():
Code:
SELECT JoinDate AS 'Date',
EXTRACT(YEAR_MONTH FROM JoinDate) AS 'Year/Month'
FROM Employee WHERE Person_ID = 101;
Output:
Explanation: Here, we have taken a database table named ‘Employee’ where the fields are Person_ID, Employee_Name, Salary, and join date. The JoinDatecolumn is the data type, so we will implement the EXTRACT() function to access the date part. Also, a condition is applied using the WHERE clause to get the date part of the particular column value in the table.
Conclusion
The MySQL EXTRACT() function needs two parameters to produce the specified part of the date or datetime. One parameter will be Unit, and next Date as in the above syntax. This function helps to retrieve only part of the required interval from date and time in MySQL through the query execution.
Recommended Articles
We hope that this EDUCBA information on “MySQL EXTRACT()” was beneficial to you. You can view EDUCBA’s recommended articles for more information.