SQL - YEAR() Function



The SQL YEAR() function returns an integer number that represents the year of the given date. The function accepts an input that can either be a literal date value or an expression that resolves to a TIME, DATE, SMALLDATETIME, DATETIME, DATETIME2, or DATETIMEOFFSET value.

YEAR() function returns the same value as DATEPART(). The DATEPART() function is a SQL built-in function that is used to extract the specified segment (as an integer) from the date value such as (year, month, or day) or time value such as (hour, minute, or second).

Note − The YEAR() function gives the same result as the DATEPART() function.

Syntax

Following is the syntax of THE SQL YEAR() function −

YEAR(date)

Parameters

This function accepts only one parameter. The same is described below −

  • date − is one of the date and time or date data types. A column name or an expression can be accepted as arguments.

Example

We can retrieve the year of the specified date by using the following query −

SELECT YEAR('2023/02/17') AS Year;

Output

When we execute the above query, the output is obtained as follows −

+------+
| Year |
+------+
| 2023 |
+------+

Example

In the following example, we are going to retrieve the year that was mentioned with a date and a time by using the following query −

SELECT YEAR('2023/02/17 02:55') AS Year;

Output

On executing the above query, the output is displayed as follows −

+------+
| Year |
+------+
| 2023 |
+------+

Example

Here, we are using YEAR() function with only time part and trying to retrieve the year and as a output it will displays 1900.

Let us look into the example, where we are using the YEAR() function with the date value containing only time part by using the following query:

SELECT YEAR('03:34:55') AS YEAR;

Output

The output for the above query is produced as given below −

+------+
| Year |
+------+
| 1900 |
+------+

Example

Look at the following example, where we are going to use the date as a parameter and variable with a YEAR() function containing time as well. Execute the following query to retrieve the year −

DECLARE @DATE VARCHAR(22);
SET @DATE = '2023/02/17 03:07';
SELECT YEAR(@DATE) AS Year;

Output

If we execute the above query, the result is produced as follows −

+------+
| Year |
+------+
| 2023 |
+------+

Example

Let us consider another scenario where we are using the variable with a YEAR() function and retrieving the year from the date specified by running the following query −

DECLARE @DATE VARCHAR(15);
SET @DATE = '2000/11/27';
SELECT YEAR(@DATE) AS Year;

Output

On executing the above query, it will generate the following output as shown below −

+------+
| Year |
+------+
| 2000 |
+------+

Example

In the following example, we are going to useCURRENT_TIMESTAMP to retrieve the year by running the following query −

SELECT YEAR(CURRENT_TIMESTAMP) AS Year;

Output

On running the above query, it will generate the output as shown below −

+------+
| Year |
+------+
| 2023 |
+------+

Example

Assume we have created a table with the name OTT in the SQL database using the CREATE statement as shown in the query below −

CREATE TABLE OTT(
ID INT NOT NULL, 
SUBSCRIBER_NAME VARCHAR (200) NOT NULL,
MEMBERSHIP VARCHAR (200),
SUBCRIPTION_DATE DATE NOT NULL);

Now, let us insert some records in the OTT table using INSERT statements as shown in the query below −

INSERT INTO OTT(ID, SUBSCRIBER_NAME, MEMBERSHIP, SUBCRIPTION_DATE) VALUES(1, 'Dhruv', 'Silver', '2022-12-05');
INSERT INTO OTT(ID, SUBSCRIBER_NAME, MEMBERSHIP, SUBCRIPTION_DATE) VALUES(2, 'Arjun','Platinum', '2021-03-01');
INSERT INTO OTT(ID, SUBSCRIBER_NAME, MEMBERSHIP, SUBCRIPTION_DATE) VALUES(3, 'Dev','Silver', '2021-03-15');
INSERT INTO OTT(ID, SUBSCRIBER_NAME, MEMBERSHIP, SUBCRIPTION_DATE) VALUES(4, 'Riya','Gold', '2022-12-05');
INSERT INTO OTT(ID, SUBSCRIBER_NAME, MEMBERSHIP, SUBCRIPTION_DATE) VALUES(5, 'Aarohi','Platinum', '2020-05-02');

We can verify whether the table OTT is created or not using the following query −

SELECT * FROM OTT;

The table was created in the database and displayed as shown below −

+----+-----------------+------------+------------------+
| ID | SUBSCRIBER_NAME | MEMBERSHIP | SUBCRIPTION_DATE |
+----+-----------------+------------+------------------+
| 1  | Dhruv           | Silver     | 2022-12-05       |
| 2  | Arjun           | Platinum   | 2021-03-01       |
| 3  | Dev             | Silver     | 2021-03-15       |
| 4  | Riya            | Gold       | 2022-12-05       |
| 5  | Aarohi          | Platinum   | 2020-05-02       |
+----+-----------------+------------+------------------+

Now, we are going to pass the column name as an argument to the YEAR() function, and retrieve the year of subscription from all the entities in the column SUBCRIPTION_DATE by running the following query −

SELECT SUBSCRIBER_NAME, YEAR(SUBCRIPTION_DATE) AS SubscribedYear FROM OTT;

Output

On executing the above query, it will generate the following output as shown below −

+-----------------+----------------+
| SUBSCRIBER_NAME | SubscribedYear |
+-----------------+----------------+
| Dhruv           | 2022           |
| Arjun           | 2021           |
| Dev             | 2021           |
| Riya            | 2022           |
| Aarohi          | 2020           |
+-----------------+----------------+

Example

Let us create a another table with the name IPLPlayers in the SQL database using the CREATE statement as shown in the query below −

CREATE TABLE IPLPlayers(
   ID INT,
   Name VARCHAR(255),
   Date_Of_Birth date,
   Country VARCHAR(255),
   PRIMARY KEY (ID)
);

Now, let us insert some records in the IPLPlayers table using INSERT statements as shown in the query below −

INSERT INTO IPLPlayers VALUES (1, 'Shikhar','1999-11-10','India');
INSERT INTO IPLPlayers VALUES (2, 'Dhoni','1992-12-11','Jharkhand');
INSERT INTO IPLPlayers VALUES (3, 'Gayle','1993-10-12','Westindies');
INSERT INTO IPLPlayers VALUES (4, 'Williamson','1992-09-23','NewZealand'); 

We can verify whether the table IPLPlayers is created or not using the following query −

SELECT * FROM IPLPlayers;

The table IPLPlayers is successfully created in the SQL database −

+----+------------+---------------+------------+
| ID | Name       | Date_Of_Birth | Country    |
+----+------------+---------------+------------+
| 1  | Shikhar    | 1999-11-10    | India      |
| 2  | Dhoni      | 1992-12-11    | Jharkhand  |
| 3  | Gayle      | 1993-10-12    | Westindies |
| 4  | Williamson | 1992-09-23    | NewZealand |
+----+------------+---------------+------------+

Now we are going to retrieve the year using the column Date_Of_Birth by running the below query −

SELECT Date_Of_Birth, YEAR(Date_Of_Birth) AS BirthYear FROM IPLPlayers;

Output

When we execute the above query, the output is obtained as follows −

+---------------+-----------+
| Date_Of_Birth | BirthYear |
+---------------+-----------+
| 1999-11-10    | 1999      |
| 1992-12-11    | 1992      |
| 1993-10-12    | 1993      |
| 1992-09-23    | 1992      |
+---------------+-----------+
sql-date-functions.htm
Advertisements