How to Round Timestamps

In MySQL if we need to convert the time fraction of the timestamp to seconds, then trunc and convert again from seconds to time. Finally we concatenate the day part of the timestamp.

SELECT daytime, 
    concat(date(daytime),' ',sec_to_time((time_to_sec(daytime) DIV 900) * 900)) rounded_to_15_min,
    concat(date(daytime),' ',sec_to_time((time_to_sec(daytime) DIV 3600) * 3600)) rounded_to_1_hour,
    concat(date(daytime),' ',sec_to_time((time_to_sec(daytime) DIV 86400) * 86400)) rounded_to_1_day
from my_sales;
rounded_to_15_min rounded_to_hour rounded_to_day
2021-07-23 11:23:00 2021-07-23 11:15:00 2021-07-23 00:00:00
2021-07-25 10:43:00 2021-07-25 10:30:00 2021-07-25 00:00:00
2021-08-16 11:25:58 2021-08-16 11:15:00 2021-08-16 00:00:00

IN THIS PAGE