w3resource

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?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/mysql-exercises/date-time-exercises/write-a-query-to-extract-the-year-from-the-current-date.php