time_round

description

Syntax

DATETIME TIME_ROUND(DATETIME expr)

DATETIME TIME_ROUND(DATETIME expr, INT period)

DATETIME TIME_ROUND(DATETIME expr, DATETIME origin)

DATETIME TIME_ROUND(DATETIME expr, INT period, DATETIME origin)

函数名 TIME_ROUND 由两部分组成,每部分由以下可选值组成

  • TIME: SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, YEAR
  • ROUND: FLOOR, CEIL

返回 expr 的上/下界。

  • period 指定每个周期有多少个 TIME 单位组成,默认为 1
  • origin 指定周期的开始时间,默认为 1970-01-01T00:00:00WEEK 的默认开始时间为 1970-01-04T00:00:00,即周日。可以比 expr 大。
  • 请尽量选择常见 period,如 3 MONTH,90 MINUTE 等,如设置了非常用 period,请同时指定 origin

example

  1. MySQL> SELECT YEAR_FLOOR('20200202000000');
  2. +------------------------------+
  3. | year_floor('20200202000000') |
  4. +------------------------------+
  5. | 2020-01-01 00:00:00 |
  6. +------------------------------+
  7. MySQL> SELECT MONTH_CEIL(CAST('2020-02-02 13:09:20' AS DATETIME), 3); --quarter
  8. +--------------------------------------------------------+
  9. | month_ceil(CAST('2020-02-02 13:09:20' AS DATETIME), 3) |
  10. +--------------------------------------------------------+
  11. | 2020-04-01 00:00:00 |
  12. +--------------------------------------------------------+
  13. MySQL> SELECT WEEK_CEIL('2020-02-02 13:09:20', '2020-01-06'); --monday
  14. +---------------------------------------------------------+
  15. | week_ceil('2020-02-02 13:09:20', '2020-01-06 00:00:00') |
  16. +---------------------------------------------------------+
  17. | 2020-02-03 00:00:00 |
  18. +---------------------------------------------------------+
  19. MySQL> SELECT MONTH_CEIL(CAST('2020-02-02 13:09:20' AS DATETIME), 3, CAST('1970-01-09 00:00:00' AS DATETIME)); --next rent day
  20. +-------------------------------------------------------------------------------------------------+
  21. | month_ceil(CAST('2020-02-02 13:09:20' AS DATETIME), 3, CAST('1970-01-09 00:00:00' AS DATETIME)) |
  22. +-------------------------------------------------------------------------------------------------+
  23. | 2020-04-09 00:00:00 |
  24. +-------------------------------------------------------------------------------------------------+

keywords

  1. TIME_ROUND