MySQL YEAR() Function
The YEAR() function in MySQL extracts the year from a given date or datetime expression and returns it as a four-digit number. It is commonly used when working with date-related data to filter or group records by year.
Syntax
- date_expression: A valid date or datetime value from which the year is extracted.
Return Value
- Returns the year as a four-digit integer (e.g., 2025).
- Returns NULLif the input isNULLor invalid.
Examples
1. Extracting Year from a Date
Output:
2. Extracting Year from a Datetime
Output:
3. Using YEAR() with NOW()
Extract the current year from the system datetime:
Output:
4. Filtering Records by Year
Retrieve records for a specific year:
5. Grouping Records by Year
Aggregate sales data by year:
Output:
Practical Use Cases
- Year-Based Filtering: - Extract and filter records based on a specific year, such as fetching orders, events, or logs.
 
- Annual Reports: - Generate summaries or trends grouped by year.
 
- Comparing Year Values: - Compare years in different columns or variables:
 
- Compare years in different columns or variables:
- Handling User-Input Dates: - Extract and use the year from user-provided dates for dynamic filtering.
 
Considerations
- Input Format: - Ensure the date_expressionis in a valid date or datetime format. If the input is invalid, the function will returnNULL.
 
- Ensure the 
- Performance: - Using YEAR()in a query may prevent the use of indexes on the column, potentially affecting performance. For better performance, consider indexing a derived column that stores the year:
 
- Using 
- NULL Values: - If the date_expressionisNULL, the function will returnNULL.
 
- If the 
Conclusion
The YEAR() function is a simple yet powerful tool for working with year values in MySQL. Whether you're filtering records, generating reports, or comparing dates, the YEAR() function helps streamline date-based operations. With proper indexing and use, it can significantly enhance the efficiency of date-related queries.

