0

我需要添加两列将时间值存储为小数示例:1.) 8.30+0.32 = 9.02(输出应该是 9.03 而不是 8.62)

上述示例运行良好,是以下 SQL 的实际输出。

我编写了以下 SQL,它主要工作正常,但我发现很少有添加不正确的情况

示例:3.57+5.25=18.44

SELECT
case when B.Column_B is null then A.Column_A
when B.Column_B is not null then 
replace(CONVERT(varchar(5), 
DATEADD(ss,(SUM((DATEPART(hh, replace(isnull(a.Column_A,0.00),'.',':'))*3600) + (DATEPART(mi,replace(isnull(a.Column_A,0.00),'.',':'))*60)) +
SUM((DATEPART(hh, replace(isnull(b.Column_B,0.00),'.',':'))*3600) + (DATEPART(mi,replace(isnull(b.Column_B,0.00),'.',':'))*60))),0),108) ,':','.')
End as "Total_Hours" 

在上述情况下,我无法找到哪里出了问题。这里有什么问题吗,或者有没有更好的方法来处理这个添加

4

2 回答 2

2

试试这个:

DECLARE @Value01 DECIMAL(9,2) = 8.30
       ,@value02 DECIMAL(9,2) = 0.32;

SELECT CONCAT 
       (
            (DATEDIFF(MINUTE, '0:00:00', CAST(REPLACE(@Value01, '.', ':') AS TIME)) +  DATEDIFF(MINUTE, '0:00:00', CAST(REPLACE(@value02, '.', ':') AS TIME))) / 60
           ,'.'
           ,RIGHT((DATEDIFF(MINUTE, '0:00:00', CAST(REPLACE(@Value01, '.', ':') AS TIME)) +  DATEDIFF(MINUTE, '0:00:00', CAST(REPLACE(@value02, '.', ':') AS TIME))) % 60 + 100, 2)
       );

首先,转换为TIME并获得总分钟数。然后,将分钟格式化为Time.使用:.

于 2020-03-06T06:24:38.923 回答
0

代码

declare @v1 DECIMAL(9,3) = 4.369,  -- 9 characters with 3 decimal places.
@v2 DECIMAL(9,3) = 3.368  -- 9 characters with 3 decimal places.
print @v1 + @v2

输出

7.737

于 2020-03-06T14:04:06.063 回答