首页 > 解决方案 > How to get the current rounded hour in MySQL?

问题描述

select current_time() gives:

16:43:16

how can I modify it to get:

16:00

I tried to play with it a bit and I wrote this:

select TIMESTAMPDIFF(HOUR,CURDATE(),current_timestamp())

Which gives:

16

I can convert it to string and add :00 but I think there might be easier way to get what I want.

Any ideas?

标签: mysqlsql

解决方案


就个人而言,我会使用DATE_FORMAT适当的格式,包括文字,例如

 SELECT DATE_FORMAT( CURRENT_TIME() ,'%H:00')

MySQL 也提供了等价的TIME_FORMAT功能

 SELECT TIME_FORMAT( CURRENT_TIME() ,'%H:00')

https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_time-format


推荐阅读