YEAR()

Description

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

Syntax

  1. > YEAR(date)
  2. > TOYEAR(date)

Arguments

ArgumentsDescription
dateRequired. The date/datetime to extract the year from

Examples

  1. drop table if exists t1;
  2. create table t1(a date, b datetime);
  3. insert into t1 values('20211223','2021-10-22 09:23:23');
  4. insert into t1 values('2021-12-23','2021-10-22 00:23:23');
  5. mysql> select year(a) from t1;
  6. +---------+
  7. | year(a) |
  8. +---------+
  9. | 2021 |
  10. | 2021 |
  11. +---------+
  12. 2 rows in set (0.00 sec)
  1. DROP TABLE IF EXISTS t3;
  2. CREATE TABLE t3(c1 DATE NOT NULL);
  3. INSERT INTO t3 VALUES('2000-01-01');
  4. INSERT INTO t3 VALUES('1999-12-31');
  5. INSERT INTO t3 VALUES('2000-01-01');
  6. INSERT INTO t3 VALUES('2006-12-25');
  7. INSERT INTO t3 VALUES('2008-02-29');
  8. mysql> SELECT YEAR(c1) FROM t3;
  9. +----------+
  10. | year(c1) |
  11. +----------+
  12. | 2000 |
  13. | 1999 |
  14. | 2000 |
  15. | 2006 |
  16. | 2008 |
  17. +----------+
  18. 5 rows in set (0.01 sec)

Constraints

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