SQL 四舍五入似乎没有按预期工作
SQL rounding does not appear to work as expected
我有以下 SQL 查询。
DECLARE
@StartDate DATE ='20160101' ,
@EndDate DATE = '20160331',
@Box1 DECIMAL,
@Box2 DECIMAL,
@Box3 DECIMAL,
@Box4 DECIMAL,
@Box5 DECIMAL,
@Box6 DECIMAL,
@Box7 DECIMAL,
@Box8 DECIMAL,
@Box9 DECIMAL
SET @Box1 = (SELECT ROUND(SUM (vt.Vat),2) FROM VatTransactions vt WHERE vt.VatTransactionDate BETWEEN @StartDate AND @EndDate)
SET @Box2 = (SELECT ROUND(SUM(vt.VatDueOnECPurchases/vt.ConversionFactor),2) FROM VatTransactions vt WHERE vt.VatTransactionDate BETWEEN @StartDate AND @EndDate)
SET @Box3 = (SELECT ROUND(SUM(@Box1 + @Box2),2))
SET @Box4 = (SELECT (ROUND(SUM(vt.VatInput),2) + @Box2) FROM VatTransactions vt WHERE vt.VatTransactionDate BETWEEN @StartDate AND @EndDate)
SET @Box5 =(SELECT @Box3 - @Box4)
SET @Box8 = (SELECT ROUND(SUM(vt.SlAway/vt.ConversionFactor),2) FROM VatTransactions vt WHERE vt.VatTransactionDate BETWEEN @StartDate AND @EndDate)
SET @Box9 = (SELECT ROUND(SUM(vt.PlAway/vt.ConversionFactor),2) FROM VatTransactions vt WHERE vt.VatTransactionDate BETWEEN @StartDate AND @EndDate)
SET @Box6 = (SELECT (ROUND(SUM(vt.SlHome),2) + @Box8) FROM VatTransactions vt WHERE vt.VatTransactionDate BETWEEN @StartDate AND @EndDate)
SET @Box7 = (SELECT (ROUND(SUM(vt.PlHome),2) + @Box9) FROM VatTransactions vt WHERE vt.VatTransactionDate BETWEEN @StartDate AND @EndDate)
SELECT @Box1 AS BOX1, @Box2 AS Box2, @Box3 AS Box3, @Box4 AS Box4, @Box5 AS Box5, @Box6 AS Box6, @Box7 AS Box7, @Box8 AS Box8, @Box9 AS Box9
当这是 运行 时,它会产生以下结果;
查询从中获取信息的表中的所有字段(日期明显除外)都定义为货币或小数类型。
实际上Box4的数字应该是1351.17,box5的数字应该是-1351.17
为什么我会看到上面的结果,我应该怎么做才能得到我期望的结果?
@Box4 DECIMAL
^^^ here is your problem
您的 DECIMAL
类型从来没有超过小数点的任何精度。因此,SQL 服务器显示小数点后精度为零位的值,即整数。
由于您似乎需要两位小数的精度,请尝试使用此声明:
@Box4 DECIMAL(10,2)
能否请您尝试以下
SET @Box4 = (SELECT (ROUND(SUM(vt.VatInput * 1.0),2) + @Box2) FROM VatTransactions vt WHERE vt.VatTransactionDate BETWEEN @StartDate AND @EndDate)
您会注意到我将列值乘以十进制数 1.0
这会将结果转换为十进制数
否则,如果 VatInput 是整数计算,它将是 integer
我有以下 SQL 查询。
DECLARE
@StartDate DATE ='20160101' ,
@EndDate DATE = '20160331',
@Box1 DECIMAL,
@Box2 DECIMAL,
@Box3 DECIMAL,
@Box4 DECIMAL,
@Box5 DECIMAL,
@Box6 DECIMAL,
@Box7 DECIMAL,
@Box8 DECIMAL,
@Box9 DECIMAL
SET @Box1 = (SELECT ROUND(SUM (vt.Vat),2) FROM VatTransactions vt WHERE vt.VatTransactionDate BETWEEN @StartDate AND @EndDate)
SET @Box2 = (SELECT ROUND(SUM(vt.VatDueOnECPurchases/vt.ConversionFactor),2) FROM VatTransactions vt WHERE vt.VatTransactionDate BETWEEN @StartDate AND @EndDate)
SET @Box3 = (SELECT ROUND(SUM(@Box1 + @Box2),2))
SET @Box4 = (SELECT (ROUND(SUM(vt.VatInput),2) + @Box2) FROM VatTransactions vt WHERE vt.VatTransactionDate BETWEEN @StartDate AND @EndDate)
SET @Box5 =(SELECT @Box3 - @Box4)
SET @Box8 = (SELECT ROUND(SUM(vt.SlAway/vt.ConversionFactor),2) FROM VatTransactions vt WHERE vt.VatTransactionDate BETWEEN @StartDate AND @EndDate)
SET @Box9 = (SELECT ROUND(SUM(vt.PlAway/vt.ConversionFactor),2) FROM VatTransactions vt WHERE vt.VatTransactionDate BETWEEN @StartDate AND @EndDate)
SET @Box6 = (SELECT (ROUND(SUM(vt.SlHome),2) + @Box8) FROM VatTransactions vt WHERE vt.VatTransactionDate BETWEEN @StartDate AND @EndDate)
SET @Box7 = (SELECT (ROUND(SUM(vt.PlHome),2) + @Box9) FROM VatTransactions vt WHERE vt.VatTransactionDate BETWEEN @StartDate AND @EndDate)
SELECT @Box1 AS BOX1, @Box2 AS Box2, @Box3 AS Box3, @Box4 AS Box4, @Box5 AS Box5, @Box6 AS Box6, @Box7 AS Box7, @Box8 AS Box8, @Box9 AS Box9
当这是 运行 时,它会产生以下结果;
查询从中获取信息的表中的所有字段(日期明显除外)都定义为货币或小数类型。
实际上Box4的数字应该是1351.17,box5的数字应该是-1351.17
为什么我会看到上面的结果,我应该怎么做才能得到我期望的结果?
@Box4 DECIMAL
^^^ here is your problem
您的 DECIMAL
类型从来没有超过小数点的任何精度。因此,SQL 服务器显示小数点后精度为零位的值,即整数。
由于您似乎需要两位小数的精度,请尝试使用此声明:
@Box4 DECIMAL(10,2)
能否请您尝试以下
SET @Box4 = (SELECT (ROUND(SUM(vt.VatInput * 1.0),2) + @Box2) FROM VatTransactions vt WHERE vt.VatTransactionDate BETWEEN @StartDate AND @EndDate)
您会注意到我将列值乘以十进制数 1.0 这会将结果转换为十进制数 否则,如果 VatInput 是整数计算,它将是 integer