YEAR

YEAR is a MySQL function that is used to extract the year from a date.

YEAR(date)

  • date: Represents the date value from which the year part is to be extracted. This can be a date type or can be a string formatted in various date-compatible formats.

Example

SELECT YEAR('2021-07-01');

Output

2021

Explanation

The YEAR function in SQL extracts the year from a date. In the above example, 2021 is extracted from the date ‘2021-07-01’.

YEAR(date)

  • date: The “date” parameter refers to a date expression from which the year is to be returned. This expression can be any of the various data types of the date and time data types category, such as DATETIME, DATE, SMALLDATETIME, etc. The function will extract the year part of the date and return it as an integer.

Example

SELECT YEAR('2022-12-31') AS YearExtracted;

Output

YearExtracted
--------------
2022

Explanation

The YEAR function in SQL Server extracts the year from a given date. In this example, the function is used to extract the year value, 2022, from the date ‘2022-12-31’.

YEAR(date)

  • date: This parameter is the input date from which the year is extracted. It should be a valid date format recognized by Oracle SQL. The YEAR function then returns the year part of this date as a four-digit format.

Example

SELECT YEAR(SYSDATE) FROM dual;

Output

2023

Explanation

The YEAR function retrieves the year component from the current system date (SYSDATE). The dual table is a special one-row table present by default in all Oracle databases.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.