📜  sql year - SQL (1)

📅  最后修改于: 2023-12-03 14:47:36.602000             🧑  作者: Mango

SQL Year

Introduction

In SQL, the YEAR function is used to extract the year from DateTime or Date data types. The function returns an integer value representing the year of the given date value.

The YEAR function is supported by most SQL database management systems including MySQL, Oracle, SQL Server, PostgreSQL, and SQLite.

Syntax

The syntax for the YEAR function is as follows:

YEAR(date);

Where date is a valid DateTime or Date value for which you want to get the year.

Example

Suppose you have a table employees with the following data:

| id | name | dob | |----|----------|------------| | 1 | John Doe | 1990-05-20 | | 2 | Jane Doe | 1985-08-30 | | 3 | Bob Ross | 1972-02-22 |

You can use the YEAR function to extract the year from the dob column as follows:

SELECT id, name, YEAR(dob) AS birth_year
FROM employees;

This will return the following output:

| id | name | birth_year | |----|----------|------------| | 1 | John Doe | 1990 | | 2 | Jane Doe | 1985 | | 3 | Bob Ross | 1972 |

Conclusion

In summary, the YEAR function is a useful tool in SQL for extracting the year from a given DateTime or Date value. It is supported by most SQL database management systems and is easy to integrate into your SQL queries.