简体   繁体   English

舍入sql整数到最近的小时

[英]round sql integer to nearest hour

I have a column formatted as an integer (values ex. 2300, 0040, 2090) with values representing military time. 我有一个格式为整数的列(值为例如2300,0040,2090),其值代表军事时间。 What would be the easiest way to round these values to the nearest hour? 将这些值舍入到最接近的小时的最简单方法是什么? So, if 2330, would round up to 2400, etc. I don't want the values converted to date/time. 所以,如果是2330,则会舍入到2400等。我不希望将值转换为日期/时间。

I'm looking for help in either SQL or Excel. 我正在寻找SQL或Excel的帮助。 Thanks. 谢谢。

Simple arithmetic. 简单算术。 Most SQL dialects support floor() : 大多数SQL方言支持floor()

select floor((col+70) / 100) * 100

In Excel, you would use: 在Excel中,您将使用:

select floor((col+70) / 100, 1) * 100

You still have an issue with 0000 and 2400. If that is an issue, then you would need to use conditional logic or modulo arithmetic. 你仍然遇到0000和2400的问题。如果这是一个问题,那么你需要使用条件逻辑或模运算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM