DAY_FLOOR
描述
DAY_FLOOR 函数用于将指定的日期或时间值向下取整(floor)到最近的指定天数周期的起点。即返回不大于输入日期时间的最大周期时刻,周期规则由 period(周期天数)和 origin(起始基准时间)共同定义。若未指定起始基准时间,默认以 0001-01-01 00:00:00 为基准计算。
日期时间的计算公式:
代表的是基准时间到目标时间的周期数
语法
DAY_FLOOR(<date_or_time_expr>)
DAY_FLOOR(<date_or_time_expr>, <origin>)
DAY_FLOOR(<date_or_time_expr>, <period>)
DAY_FLOOR(<date_or_time_expr>, <period>, <origin>)
参数
参数 | 说明 |
---|---|
<date_or_time_expr> | 参数是合法的日期表达式,支持输入 date/datetime 类型,具体 datetime 和 date 格式请查看 datetime 的转换 和 date 的转换 |
<period> | 参数是指定每个周期包含的天数,类型为 INT。若为负数或 0,返回 错误;若未指定,默认周期为 1 天。 |
<origin> | 参数是周期计算的起始基准时间,支持 date/datetime 类型。若未指定,默认值为 0001-01-01 00:00:00;若输入无效格式,返回 NULL。 |
返回值
返回的是一个日期或时间值,表示将输入值向下舍入到指定天数周期的结果。
若输入有效,返回与 datetime 类型一致的取整结果:
<date_or_time_expr>
与 <origin>
输入都 DATE 类型时,返回 DATE 类型, 否则返回 DATETIME 类型.
特殊情况:
- 任何参数为 NULL 时,返回 NULL;
- 若 period 为负数或 0,返回错误
- 带有 scale 输入的符合日期时间,返回值带有 scale 且全部小数为 0
- 若
<origin>
日期时间在<period>
之后,也会按照上述公式计算,不过周期 k 为负数。
举例
---五天为一周期向下取整
select day_floor("2023-07-13 22:28:18", 5);
+-------------------------------------+
| day_floor("2023-07-13 22:28:18", 5) |
+-------------------------------------+
| 2023-07-10 00:00:00 |
+-------------------------------------+
---带有 scale 输入的符合日期时间,返回值带有 scale 且全部小数为 0
mysql> select day_floor("2023-07-13 22:28:18.123", 5);
+-----------------------------------------+
| day_floor("2023-07-13 22:28:18.123", 5) |
+-----------------------------------------+
| 2023-07-10 00:00:00.000 |
+-----------------------------------------+
---输入参数不带有周期,默认一天为一周期
select day_floor("2023-07-13 22:28:18");
+----------------------------------+
| day_floor("2023-07-13 22:28:18") |
+----------------------------------+
| 2023-07-13 00:00:00 |
+----------------------------------+
---指定周期为 7 天(1 周),自定义基准时间为 2023-01-01 00:00:00
select day_floor("2023-07-13 22:28:18", 7, "2023-01-01 00:00:00");
+------------------------------------------------------------+
| day_floor("2023-07-13 22:28:18", 7, "2023-01-01 00:00:00") |
+------------------------------------------------------------+
| 2023-07-09 00:00:00 |
+------------------------------------------------------------+
---开始时间恰好在一个周期开始,则返回输入日期时间
select day_floor("2023-07-09 00:00:00", 7, "2023-01-01 00:00:00");
+------------------------------------------------------------+
| day_floor("2023-07-09 00:00:00", 7, "2023-01-01 00:00:00") |
+------------------------------------------------------------+
| 2023-07-09 00:00:00 |
+------------------------------------------------------------+
---输入为 DATE 类型,周期为 3 天
select day_floor(cast("2023-07-13" as date), 3);
+------------------------------------------+
| day_floor(cast("2023-07-13" as date), 3) |
+------------------------------------------+
| 2023-07-11 |
+------------------------------------------+
--- 若 <origin> 日期时间在 <period> 之后,也会按照上述公式计算,不过周期 k 为负数。
select day_floor('2023-07-13 19:30:00.123', 4, '2028-07-14 08:00:00');
+----------------------------------------------------------------+
| day_floor('2023-07-13 19:30:00.123', 4, '2028-07-14 08:00:00') |
+----------------------------------------------------------------+
| 2023-07-13 08:00:00.000 |
+----------------------------------------------------------------+
---周期为负数,返回错误
select day_floor("2023-07-13 22:28:18", -2);
ERROR 1105 (HY000): errCode = 2, detailMessage = (10.16.10.3)[E-218]Operation day_floor of 2023-07-13 22:28:18, -2 out of range
---任意参数为 NULL ,返回 NULL
select day_floor(NULL, 5, "2023-01-01");
+----------------------------------+
| day_floor(NULL, 5, "2023-01-01") |
+----------------------------------+
| NULL |
+----------------------------------+