Equation在SQL Server中保持返回int

时间:2017-07-27 08:57:00

标签: sql sql-server

使用以下select查询:

SELECT DATEDIFF(day, cd.LastTestDate, cd.date) / 365.0 AS DateDifference, CAST(1.0 - (cd.DeclRate * ((DATEDIFF(day, cd.LastTestDate, cd.date))/365.0))AS FLOAT) AS DeclRate 
FROM V_INT_PROD_PLAN2 cd 

我得到以下结果:

DateDifference    DeclRate
0.000000           1
0.002739           1
0.005479           1

如何将第二列作为小数返回?任何指针都会非常感激。

1 个答案:

答案 0 :(得分:2)

第二列中的一个值是为您提供一个导致int的值,而int又会将输出转换为DECLARE @DeclRate AS NUMERIC(20, 8); DECLARE @FromDate AS DATETIME; DECLARE @ToDate AS DATETIME; SET @DeclRate = 0.000000123; SET @FromDate = GETDATE(); SET @ToDate = GETDATE() + 1; -- not int result SELECT CAST(1.0 - (@DeclRate * ((DATEDIFF(DAY, @FromDate, @ToDate)) / 365.0)) AS FLOAT) AS DeclRate; -- set variable to 0 value SET @DeclRate = 0.0000; -- output is int result SELECT CAST(1.0 - (@DeclRate * ((DATEDIFF(DAY, @FromDate, @ToDate)) / 365.0)) AS FLOAT) AS DeclRate; -- set dates to be same day SET @ToDate = GETDATE(); SET @DeclRate = 0.000000123; -- output is int result SELECT CAST(1.0 - (@DeclRate * ((DATEDIFF(DAY, @FromDate, @ToDate)) / 365.0)) AS FLOAT) AS DeclRate; 。见以下样本:

@DeclRate * (datediff...

我怀疑您正在进行的日期比较是比较相同的日期,因此0将始终导致输出 documet.getElementById ('demo'). InnerHtml