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)
The function name TIME_ROUND consists of two parts, Each part consists of the following optional values.
TIME:SECOND,MINUTE,HOUR,DAY,WEEK,MONTH,YEARROUND:FLOOR,CEIL
Returns the upper/lower bound of expr.
periodspecifies how manyTIMEunits, the default is1.originspecifies the start time of the period, the default is1970-01-01T00:00:00, the start time ofWEEKis Sunday, which is1970-01-04T00:00:00. Could be larger thanexpr.- Please try to choose common
period, such as 3MONTH, 90MINUTE. If you set a uncommonperiod, please also specifyorigin.
example
2. MySQL> SELECT YEAR_FLOOR('20200202000000');
3. +------------------------------+
4. | year_floor('20200202000000') |
5. +------------------------------+
6. | 2020-01-01 00:00:00 |
7. +------------------------------+
10. MySQL> SELECT MONTH_CEIL(CAST('2020-02-02 13:09:20' AS DATETIME), 3); --quarter
11. +--------------------------------------------------------+
12. | month_ceil(CAST('2020-02-02 13:09:20' AS DATETIME), 3) |
13. +--------------------------------------------------------+
14. | 2020-04-01 00:00:00 |
15. +--------------------------------------------------------+
18. MySQL> SELECT WEEK_CEIL('2020-02-02 13:09:20', '2020-01-06'); --monday
19. +---------------------------------------------------------+
20. | week_ceil('2020-02-02 13:09:20', '2020-01-06 00:00:00') |
21. +---------------------------------------------------------+
22. | 2020-02-03 00:00:00 |
23. +---------------------------------------------------------+
26. 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
27. +-------------------------------------------------------------------------------------------------+
28. | month_ceil(CAST('2020-02-02 13:09:20' AS DATETIME), 3, CAST('1970-01-09 00:00:00' AS DATETIME)) |
29. +-------------------------------------------------------------------------------------------------+
30. | 2020-04-09 00:00:00 |
31. +-------------------------------------------------------------------------------------------------+
keyword
TIME_ROUND
