在十进制sql server中获取datediff小时

时间:2022-06-17 08:53:39

i need query to get total hours in decimal value

我需要查询以获得十进制值的总小时数

this is my query so far:

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

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

虽然@total参数是小数,但查询给出了整数值。我不知道如何获取小数值。

please help me, and thank you in advance.

请帮帮我,并提前谢谢你。

2 个解决方案

#1


3  

Try divide by 60.0. This will provide the required precision

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

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

int除以int将返回int。为了避免这种情况,只需将分子或分母变为浮点数即可。

Example

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

Returns

返回

1.50

#2


0  

DateDiff always return int, so take the DateDiff in minute, and then divide by 60.0. The decimal point is required.

DateDiff总是返回int,所以以分钟为单位取DateDiff,然后除以60.0。小数点是必需的。

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

#1


3  

Try divide by 60.0. This will provide the required precision

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

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

int除以int将返回int。为了避免这种情况,只需将分子或分母变为浮点数即可。

Example

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

Returns

返回

1.50

#2


0  

DateDiff always return int, so take the DateDiff in minute, and then divide by 60.0. The decimal point is required.

DateDiff总是返回int,所以以分钟为单位取DateDiff,然后除以60.0。小数点是必需的。

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