MySQL Date and Time Exercises: Extract the year from the current date
MySQL Date Time: Exercise-9 with Solution
Write a MySQL query to extract the year from the current date.
Code:
-- This SQL query extracts the year component from the current date and time.
SELECT
EXTRACT(YEAR FROM NOW());
Explanation:
- The EXTRACT() function is used to extract a specific component (in this case, the year) from a date or datetime expression.
- YEAR is specified as the component to extract from the current date and time returned by the NOW() function.
- The result of this query will be the current year extracted from the current date and time.
Sample Output:
EXTRACT(YEAR FROM NOW()) 2017
MySQL Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous:Write a MySQL query to get the current date in Thursday September 2014 format.
Next:Write a MySQL query to get the DATE value from a given day (number in N).
What is the difficulty level of this exercise?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics