SQL中的小时计算 [英] Hours Calculation in SQL

查看:183
本文介绍了SQL中的小时计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这样做.. !!!



DECLARE @minutes AS INT



SET @minutes = 1.80 * 100



SELECT @minutes / 60 +(@minutes%60)/ 100.0 AS Hours



它给了我3.000但结果应该是2.20小时



我怎么能实现那个......?



请帮我解决

解决方案

整数。有趣的小恶魔不是他们......

什么是1.8 * 100?

答案:180

什么是180/60?

答案:3

什么是180%60?

答案:0



所以首先答案是3,而不是2-and-bit。



其次,当你将整数除以小于100(任何数字%60将始终是)100?

答案:0,因为整数算术按定义丢弃了数字的一小部分...



所以,你可能想要考虑在整个浮点数工作...


  DECLARE  @minutes INT 
DECLARE @hours INT
DECLARE @time decimal 5 2 )= 1 . 80

SET @hours = floor(@time)
SET @minutes =(@ time - @hours)* 100
< span class =code-keyword> SET @minutes = @minutes +(@hours * 60

SELECT @minutes / 60 +(@min utes% 60 )/ 100 0 AS 小时





这个怎么样.......! !!!


I'm doing like this..!!!

DECLARE @minutes AS INT

SET @minutes = 1.80*100

SELECT @minutes / 60 + (@minutes % 60) / 100.0 AS Hours

it gives me 3.000 but the result should be 2.20 hrs

how can i acheive that..?

pls help me out

解决方案

Integers. Interesting little devils aren't they...
What is 1.8 * 100?
Answer: 180
What is 180 / 60?
Answer: 3
What is 180 % 60?
Answer: 0

So first off the answer is 3, not 2-and-a-bit at all.

Secondly, What do you get when you integer divide a number less than 60 (as any number % 60 will always be) by 100?
Answer: 0, because integer arithmetic throws away fraction parts of numbers by definition...

So, probably, you want to consider working in floating point numbers throughout...


DECLARE @minutes INT
DECLARE @hours INT
DECLARE @time decimal(5,2) = 1.80

SET @hours = floor(@time)
SET @minutes = (@time - @hours) * 100
SET @minutes = @minutes + (@hours * 60)

SELECT  @minutes / 60 + (@minutes % 60) / 100.0 AS Hours



How about this.......!!!!


这篇关于SQL中的小时计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆