Skip to content

HOUR()

函数说明

返回时间的小时。对于时间值,返回值的范围是 0 到 23。但是,time 值的范围实际上要大得多,因此 HOUR 可以返回大于 23 的值。如果 timeNULL 则返回 NULL

函数语法

> HOUR(time)

参数释义

参数 说明
time 必要参数。表示时间或时间戳的值。

示例

drop table if exists t1;
create table t1(a datetime, b timestamp);
insert into t1 values("2022-07-01", "2011-01-31 12:00:00");
insert into t1 values("2011-01-31 12:32:11", "1979-10-22");
insert into t1 values(NULL, "2022-08-01 23:10:11");
insert into t1 values("2011-01-31", NULL);
insert into t1 values("2022-06-01 14:11:09","2022-07-01 00:00:00");
insert into t1 values("2022-12-31","2011-01-31 12:00:00");
insert into t1 values("2022-06-12","2022-07-01 00:00:00");

mysql> select hour(a),hour(b) from t1;
+---------+---------+
| hour(a) | hour(b) |
+---------+---------+
|       0 |      12 |
|      12 |       0 |
|    NULL |      23 |
|       0 |    NULL |
|      14 |       0 |
|       0 |      12 |
|       0 |       0 |
+---------+---------+
7 rows in set (0.00 sec)

mysql> select * from t1 where hour(a)>hour(b);
+---------------------+---------------------+
| a                   | b                   |
+---------------------+---------------------+
| 2011-01-31 12:32:11 | 1979-10-22 00:00:00 |
| 2022-06-01 14:11:09 | 2022-07-01 00:00:00 |
+---------------------+---------------------+
2 rows in set (0.01 sec)