MySQL

How To Get Year From Date In MySQL

In SQL language the YEAR() function allows extracting a year from a date in YYYY-MM-DD format.
 

Syntax:

To use this function in a SQL query, the following syntax must be respected:

SELECT YEAR( date );

In this example, the “date” parameter can be a date such as ‘2022-01-01’ and will return a 4 character string.
 

 

Example :

The following queries show the use of the function in a practical context:

SELECT YEAR('2022-02-15'); -- output: 2022
SELECT YEAR('2000-01-13'); -- output: 2000
SELECT YEAR('1994-07-09'); -- output: 1994
SELECT YEAR('2024-12-30'); -- output: 2024

Note: if the input date respects the YYYY-MM-DD format, then the result can also be obtained using the LEFT() function by truncating the date to the first 4 characters or by using the SUBSTR() or SUBSTRING() function by also truncating the first characters.
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *