Skip to content

YEAR()

Description

The YEAR() or TOYEAR() function returns the year part for a given date (a number from 1000 to 9999).

Syntax

> YEAR(date)
> TOYEAR(date)

Arguments

Arguments Description
date Required. The date/datetime to extract the year from

Examples

drop table if exists t1;
create table t1(a date, b datetime);
insert into t1 values('20211223','2021-10-22 09:23:23');
insert into t1 values('2021-12-23','2021-10-22 00:23:23');

mysql> select year(a) from t1;
+---------+
| year(a) |
+---------+
|    2021 |
|    2021 |
+---------+
2 rows in set (0.00 sec)

sql <!-- validator-ignore-exec --> DROP TABLE IF EXISTS t3; CREATE TABLE t3(c1 DATE NOT NULL); INSERT INTO t3 VALUES('2000-01-01'); INSERT INTO t3 VALUES('1999-12-31'); INSERT INTO t3 VALUES('2000-01-01'); INSERT INTO t3 VALUES('2006-12-25'); INSERT INTO t3 VALUES('2008-02-29'); mysql> SELECT YEAR(c1) FROM t3; +----------+ | year(c1) | +----------+ | 2000 | | 1999 | | 2000 | | 2006 | | 2008 | +----------+ 5 rows in set (0.01 sec)

Constraints

The date type supports only yyyy-mm-dd and yyyymmdd for now.