YEAR()¶
Function Description¶
The YEAR() and TOYEAR() functions return the year (from 1000 to 9999) of the given date.
Function Syntax¶
> YEAR(date)
> TOYEAR(date)
Parameter definition¶
Parameters |
Description |
|---|---|
date |
Required parameter, date of year that needs to be extracted |
Example¶
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)
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)
limit¶
Currently, only the data formats of yyyy-mm-dd and yyyymmdd are supported.