EXTRACT() Function in MySQL
Last Updated :
11 Sep, 2024
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 MySQL
- The
EXTRACT()
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.
- This function allows us to extract parts such as year, month, day, hour, minute, second etc.
Syntax:
EXTRACT(unit FROM date)
Parameter:
This method accepts two parameters which are illustrated below:
- part - Specified part to extract like SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, YEAR, etc.
- date - Specified date to extract a part from.
Returns:
It returns the desired extracted part from a specified date.
Examples of EXTRACT() Function in MySQL
Example 1: Extracting the Year
To 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:
- The
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.
2. Improves Query Readability:
- By using
EXTRACT()
, queries that deal with dates become more readable and maintainable.
- We can easily understand the intent when extracting specific parts of a date or time.
3. Efficient Data Filtering:
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.
4. Supports Various Date/Time Units:
- The function supports a wide range of units (e.g., year, quarter, week, day, hour), offering flexibility when dealing with different date and time-related requirements.
5. Works with Multiple Data Types
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.
Conclusion
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.
Similar Reads
ELT() Function in MySQL In this article, we are going to cover ELT function with examples. In ELT function, number field will state that how many strings will be there. ELT function in MySQL is used to returns the string which is at index number specified in the argument list. In this function there is number field and str
1 min read
COT() Function in MySQL COT() function : This function in MySQL is used to return the cotangent of a specified number. If the specified number is 0, an error or NULL will be returned. In a right triangle, the cotangent of an angle is the length of it's adjacent side divided by the length of the opposite side. Similarly, th
1 min read
EXP() Function in MySQL EXP() function in MySQL is used to returns E raised to the power of a specified number. Here E(2.718281...) is the base of the natural logarithm. Syntax : EXP(X) Parameter : This method accepts one parameter as mentioned above in the syntax and described below : X â A specified number which will be
2 min read
PLSQL | EXTRACT Function The PLSQL EXTRACT function is used for extracting a specific value such as year, month, day or hour from a date or an interval value. Syntax: EXTRACT(field FROM source) Parameters Used: The EXTRACT function accepts two parameters : field - It is used to specify the component that needs to be extract
2 min read
DAY() Function in MySQL DAY() function : This function in MySQL is used to return the day of the month for a specified date (a number from 1 to 31). This function equals the DAYOFMONTH() function. Syntax : DAY(date) Parameter : This method accepts a parameter which is illustrated below : date : Specified date to extract th
1 min read
ASCII() Function in MySQL In this article, we are going to cover the ASCII function with examples and you will see the ASCII MYSQL query. And will also cover the ASCII code for the given character. Let's discuss one by one. ASCII function in MySQL is used to find the ASCII code of the leftmost character of a character expres
1 min read
EXPORT_SET() function in MySQL EXPORT_SET() : This function helps to return a string which will show the bits in number. The function requires 5 arguments for its functioning. The function converts first argument i.e integer to binary digits, then returns âonâ if binary digit is 1 and âoffâ if binary digit is 0. Syntax : EXPORT_S
2 min read
COUNT() Function in MySQL The COUNT() function in MySQL is a versatile aggregate function used to determine the number of rows or non-NULL values that match a specific condition in a query. It can be applied to an entire table or a particular column and is widely used in database operations to analyze the volume of data in a
3 min read
BIT_AND() function in MySQL BIT_AND() : This function in MySQL is used to return the Bitwise AND of all bits in a given expression. It first converts all decimal values into binary values, and then perform bitwise and operation on those binary values. The BIT_AND() function works by performing a bitwise AND operation on each p
3 min read
CURTIME() function in MySQL CURTIME() function in MySQL is used to check the current time. It returns the current time as a value in âhh:mm:ssâ or 'hhmmss' format, depending on whether the function is used in a string or numeric context. Syntax : CURTIME(fsp) Parameters : This method accepts only one parameter. fsp - It specif
2 min read