HOUR()

Description

Returns the hour for time. The range of the return value is 0 to 23 for time-of-day values. However, the range of TIME values actually is much larger, so HOUR can return values greater than 23. Returns NULL if time is NULL.

Syntax

  1. > HOUR(time)

Arguments

ArgumentsDescription
timeRequired. A value that represents time or timestamp.

Examples

  1. drop table if exists t1;
  2. create table t1(a datetime, b timestamp);
  3. insert into t1 values("2022-07-01", "2011-01-31 12:00:00");
  4. insert into t1 values("2011-01-31 12:32:11", "1979-10-22");
  5. insert into t1 values(NULL, "2022-08-01 23:10:11");
  6. insert into t1 values("2011-01-31", NULL);
  7. insert into t1 values("2022-06-01 14:11:09","2022-07-01 00:00:00");
  8. insert into t1 values("2022-12-31","2011-01-31 12:00:00");
  9. insert into t1 values("2022-06-12","2022-07-01 00:00:00");
  10. mysql> select hour(a),hour(b) from t1;
  11. +---------+---------+
  12. | hour(a) | hour(b) |
  13. +---------+---------+
  14. | 0 | 12 |
  15. | 12 | 0 |
  16. | NULL | 23 |
  17. | 0 | NULL |
  18. | 14 | 0 |
  19. | 0 | 12 |
  20. | 0 | 0 |
  21. +---------+---------+
  22. 7 rows in set (0.00 sec)
  23. mysql> select * from t1 where hour(a)>hour(b);
  24. +---------------------+---------------------+
  25. | a | b |
  26. +---------------------+---------------------+
  27. | 2011-01-31 12:32:11 | 1979-10-22 00:00:00 |
  28. | 2022-06-01 14:11:09 | 2022-07-01 00:00:00 |
  29. +---------------------+---------------------+
  30. 2 rows in set (0.01 sec)