跳到主要内容

HOUR_CEIL

描述

HOUR_CEIL 函数用于将输入的日期时间值向上取整到指定小时周期的最近时刻。例如,若指定周期为 5 小时,函数会将输入时间调整为该周期内的下一个整点时刻(若输入时间已在周期起点,则保持不变)。 日期计算公式:

hour_ceil(date_or_time_expr,period,origin)=min{origin+k×period×hourkZorigin+k×period×hourdate_or_time_expr}\begin{aligned} &\text{hour\_ceil}(\langle\text{date\_or\_time\_expr}\rangle, \langle\text{period}\rangle, \langle\text{origin}\rangle) = \\ &\min\{\langle\text{origin}\rangle + k \times \langle\text{period}\rangle \times \text{hour} \mid \\ &k \in \mathbb{Z} \land \langle\text{origin}\rangle + k \times \langle\text{period}\rangle \times \text{hour} \geq \langle\text{date\_or\_time\_expr}\rangle\} \end{aligned}

kk 代表基准时间到达目标时间所需的周期数

语法

HOUR_CEIL(`<date_or_time_expr>`)
HOUR_CEIL(`<date_or_time_expr>`, `<origin>`)
HOUR_CEIL(`<date_or_time_expr>`, `<period>`)
HOUR_CEIL(`<date_or_time_expr>`, `<period>`, `<origin>`)

参数

参数说明
<date_or_time_expr>参数是合法的日期表达式,支持输入 datetime 和 date 类型,date 类型会转换为一天的 00:00:00 开始,具体 datetime/date 格式请查看 datetime 的转换date 的转换
<period>可选参数,指定周期长度(单位:小时),为正整数(如 1、3、5)。默认值为 1,表示每 1 小时一个周期
<origin>开始的时间起点,支持输入 datetime 和 date 类型,如果不填,默认是 0001-01-01T00:00:00

返回值

返回 DATETIME 类型的值,表示向上取整后的最近周期时刻。

  • 若输入的 period 为非正数,返回错误。
  • 若是任意参数为 NULL ,结果返回 NULL.
  • origin 或 datetime 带有 scale, 返回结果带有 scale, 小数部位变为零
  • 计算结果大于最大日期时间范围 9999-12-31 23:59:59, 返回 错误
  • <origin> 日期时间在 <period> 之后,也会按照上述公式计算,不过周期 k 为负数。

举例



--指定五小时为周期向上取整
mysql> select hour_ceil("2023-07-13 22:28:18", 5);
+-------------------------------------+
| hour_ceil("2023-07-13 22:28:18", 5) |
+-------------------------------------+
| 2023-07-13 23:00:00 |
+-------------------------------------+

--- 以2023-07-13 08:00为起点,按4小时周期划分
mysql> select hour_ceil('2023-07-13 19:30:00', 4, '2023-07-13 08:00:00') as custom_origin;
+----------------------------+
| custom_origin |
+----------------------------+
| 2023-07-13 20:00:00 |
+----------------------------+

--输入 date 类型,则会转换为 对应日期的起点时间 00:00:00
mysql> select hour_ceil('2023-07-13 00:30:00', 6, '2023-07-13');
+---------------------------------------------------+
| hour_ceil('2023-07-13 00:30:00', 6, '2023-07-13') |
+---------------------------------------------------+
| 2023-07-13 06:00:00 |
+---------------------------------------------------+

---恰好在一个周期的边缘,则返回输入的日期时间
select hour_ceil('2023-07-13 01:00:00');
+----------------------------------+
| hour_ceil('2023-07-13 01:00:00') |
+----------------------------------+
| 2023-07-13 01:00:00 |
+----------------------------------+

--- origin 或 datetime 带有 scale,返回结果带有 scale
mysql> select hour_ceil('2023-07-13 19:30:00', 4, '2023-07-13 08:00:00.123') ;
+----------------------------------------------------------------+
| hour_ceil('2023-07-13 19:30:00', 4, '2023-07-13 08:00:00.123') |
+----------------------------------------------------------------+
| 2023-07-13 20:00:00.123 |
+----------------------------------------------------------------+

mysql> select hour_ceil('2023-07-13 19:30:00.123', 4, '2023-07-13 08:00:00') ;
+----------------------------------------------------------------+
| hour_ceil('2023-07-13 19:30:00.123', 4, '2023-07-13 08:00:00') |
+----------------------------------------------------------------+
| 2023-07-13 20:00:00.000 |
+----------------------------------------------------------------+

--- 若 <origin> 日期时间在 <period> 之后,也会按照上述公式计算,不过周期 k 为负数。
select hour_ceil('2023-07-13 19:30:00.123', 4, '2028-07-14 08:00:00');
+----------------------------------------------------------------+
| hour_ceil('2023-07-13 19:30:00.123', 4, '2028-07-14 08:00:00') |
+----------------------------------------------------------------+
| 2023-07-13 20:00:00.000 |
+----------------------------------------------------------------+

---计算结果大于最大日期时间范围 9999-12-31 23:59:59,返回 NULL
select hour_ceil("9999-12-31 22:28:18", 6);
ERROR 1105 (HY000): errCode = 2, detailMessage = (10.16.10.3)[E-218]Operation hour_ceil of 9999-12-31 22:28:18, 6 out of range

---period 小于等于 0.返回错误
mysql> select hour_ceil("2023-07-13 22:28:18", 0);
ERROR 1105 (HY000): errCode = 2, detailMessage = (10.16.10.3)[E-218]Operation hour_ceil of 2023-07-13 22:28:18, 0 out of range

---任意输入参数为 NULL,返回 NULL
mysql> select hour_ceil(null, 3) as null_input;
+------------+
| null_input |
+------------+
| NULL |
+------------+

mysql> select hour_ceil("2023-07-13 22:28:18", NULL);
+----------------------------------------+
| hour_ceil("2023-07-13 22:28:18", NULL) |
+----------------------------------------+
| NULL |
+----------------------------------------+

mysql> select hour_ceil("2023-07-13 22:28:18", 5,NULL);
+------------------------------------------+
| hour_ceil("2023-07-13 22:28:18", 5,NULL) |
+------------------------------------------+
| NULL |
+------------------------------------------+