获取十进制SQL Server中的datediff小时 [英] get datediff hours in decimal sql server

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

问题描述

我需要查询以十进制值表示总小时数

i need query to get total hours in decimal value

declare @start as time;
declare @end as time;
declare @total as decimal(18, 2);

set @start = '17:00:00'
set @end = '18:30:00'

set @total = datediff(minute, @start, @end)/60

print @total

查询给我一个整数值,尽管@total参数是一个小数。
我不知道如何获取十进制值。

the query give me integer value, although the @total parameter is a decimal. I don't know how to get the decimal value.

请帮助我,谢谢您。

推荐答案

尝试除以60.0。这将提供所需的精度

Try divide by 60.0. This will provide the required precision

一个整数除以一个整数将返回一个整数。要避免这种情况,只需将分子或分母变成浮点数即可。

An int divided by an int will return an int. To circumvent this, simply make either the numerator or denominator into a float.

示例

declare @start as time;
declare @end as time;
declare @total as decimal(18, 2);

set @start = '17:00:00'
set @end = '18:30:00'

set @total = datediff(minute, @start, @end)/60.0

print @total

返回

1.50

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

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