YEAR()

函数说明

YEAR() 和 TOYEAR() 函数返回了给定日期的年份(从 1000 到 9999)。

函数语法

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

参数释义

参数 说明
date 必要参数,需要提取年份的日期

示例

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');

6. mysql> select year(a) from t1;
7. +---------+
8. | year(a) |
9. +---------+
10. |    2021 |
11. |    2021 |
12. +---------+
13. 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)

限制

目前只支持 yyyy-mm-dd 和 yyyymmddd 的数据格式。