EDUCBA

EDUCBA

MENUMENU
  • Explore
    • Lifetime Membership
    • All in One Bundles
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Login
Home Data Science Data Science Tutorials MySQL Tutorial MySQL EXTRACT()

MySQL EXTRACT()

Aanchal Sharma
Article byAanchal Sharma
Priya Pedamkar
Reviewed byPriya Pedamkar

Updated May 12, 2023

MySQL EXTRACT()

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.

ADVERTISEMENT
Popular Course in this category
MYSQL Course Bundle - 18 Courses in 1 | 3 Mock Tests

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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:

MySQL EXTRACT()1

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:

MySQL EXTRACT()2

3. Abstract DAY_MICROSECOND using EXTRACT()

Code:

SELECT EXTRACT(DAY_MICROSECOND FROM '2020-05-10 06:20:45') DayMicrosecond

Output:

MySQL EXTRACT()3

4. Abstract DAY_MINUTE using EXTRACT()

Code:

SELECT EXTRACT(DAY_MINUTE FROM '2020-05-10 06:20:45') DAYMINUTE;

Output:

MySQL EXTRACT()4

5. Abstract DAY_SECOND using EXTRACT()

Code:

SELECT EXTRACT(DAY_SECOND FROM '2020-05-10 06:20:45') DAYSECOND;

Output:

MySQL EXTRACT()5

6. Abstract HOUR using EXTRACT()

Code:

SELECT EXTRACT(HOUR FROM '2020-05-10 06:20:45') HOUR;

Output:

MySQL EXTRACT()6

7. Abstract HOUR_MICROSECOND using EXTRACT()

Code:

SELECT EXTRACT(HOUR_MICROSECOND FROM '2020-05-10 06:20:45') HOURMICROSECOND;

Output:

MySQL EXTRACT()7

8. Abstract HOUR_MINUTE using EXTRACT()

Code:

SELECT EXTRACT(HOUR_MINUTE FROM '2020-05-10 06:20:45') HOURMINUTE;

Output:

MySQL EXTRACT()8

9. Abstract HOUR_SECOND using EXTRACT()

Code:

SELECT EXTRACT(HOUR_SECOND FROM '2020-05-10 06:20:45') HOURSECOND;

Output:

MySQL EXTRACT()9

10. Abstract MICROSECOND using EXTRACT()

Code:

SELECT EXTRACT(MICROSECOND FROM '2020-05-10 06:20:45') MICROSECOND;

Output:

MySQL EXTRACT()10

11. Abstract MINUTE using EXTRACT()

Code:

SELECT EXTRACT(MINUTE FROM '2020-05-10 06:20:45') MINUTE;

Output:

Abstract MINUTE

12. Abstract MINUTE_MICROSECOND using EXTRACT()

Code:

SELECT EXTRACT(MINUTE_MICROSECOND FROM '2020-05-10 06:20:45') MINUTEMICROSECOND;

Output:

MICROSECOND

13. Abstract MINUTE_SECOND using EXTRACT()

Code:

SELECT EXTRACT(SECOND FROM '2020-05-10 06:20:45') SECOND;

Output:

SECOND

14. Abstract MONTH using EXTRACT()

Code:

SELECT EXTRACT(MONTH FROM '2020-05-10') MONTH;

Output:

MONTH

15. Abstract QUARTER using EXTRACT()

Code:

SELECT EXTRACT(QUARTER FROM '2020-05-10 06:20:45') QUARTER;

Output:

QUARTER

16. Abstract SECOND_MICROSECOND using EXTRACT()

Code:

SELECT EXTRACT(SECOND_MICROSECOND FROM '2020-05-10 06:20:45') SECONDMICROSECOND;

Output:

Abstract SECOND_MICROSECOND

17. Abstract WEEK using EXTRACT()

Code:

SELECT EXTRACT(WEEK FROM '2020-05-10') WEEK;

Output:

WEEK

18. Abstract YEAR using EXTRACT()

Code:

SELECT EXTRACT(YEAR FROM '2020-05-10 06:20:45') YEAR;

Output:

YEAR

19. Abstract YEAR_MONTH using EXTRACT()

Code:

SELECT EXTRACT(YEAR_MONTH FROM '2020-05-10 06:20:45') YEARMONTH;

Output:

YEARMONTH

20. Abstract HOUR_MICROSECOND using EXTRACT()

Code:

SELECT EXTRACT(HOUR_MICROSECOND FROM '2020-05-10 06:20:45.000001') HOURMICROSECOND;

Output:

HOUR MICROSECOND

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:

combination of Unit Specifiers

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:

Current DATE/TIME function

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:

MySQL EXTRACT()23

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.

  1. IF Statement in MySQL
  2. MySQL Timestamp
  3. ROLLUP in MySQL
  4. MySQL Self Join
ADVERTISEMENT
C++ PROGRAMMING Course Bundle - 9 Courses in 1 | 5 Mock Tests
40+ Hour of HD Videos
9 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
ASP.NET Course Bundle - 28 Courses in 1 | 5 Mock Tests
123+ Hours of HD Videos
28 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
SQL Course Bundle - 51 Courses in 1 | 6 Mock Tests
205+ Hours of HD Videos
51 Courses
6 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
SOFTWARE TESTING Course Bundle - 13 Courses in 1
53+ Hour of HD Videos
13 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Extended Cyber Monday Price Drop! All in One Universal Bundle (3700+ Courses) @ 🎁 90% OFF - Ends in ENROLL NOW