0

我正在尝试做看似基本的 int 到小数的转换,但它不起作用。我已经尝试在插入之前执行演员表,没有更好的。我想使用十进制值来显示百分比变化。代码如下,连同输出。任何帮助都会很棒..

DECLARE @tblVar TABLE (LatestCount INT, PriorCount INT, 
                       PctChgLatestVsPrior DECIMAL, PctChgLatestVsAvg DECIMAL)

DECLARE @tbl CHAR(30)
DECLARE @dte DATE
DECLARE @dte2 DATE
DECLARE @latestCount INT
DECLARE @priorCount INT
DECLARE @avgAllCounts INT

SET @tbl = 'tblDailyPricingAndVol'
SET @dte = '5/8/12'
SET @dte2 = '5/7/12'

EXEC spAvgOfCountByDateAddedByTable @tbl, @avg = @avgAllCounts OUTPUT
EXEC spCountOfDateAddedByTableAndDate @tbl, @dte, @count = @latestCount OUTPUT
EXEC spCountOfDateAddedByTableAndDate @tbl, @dte2, @count = @priorCount OUTPUT

INSERT INTO @tblVar 
VALUES (@latestCount, @priorCount, 
    CAST((@latestCount-@priorCount) AS DECIMAL)/CAST(@priorCount AS DECIMAL), 
    CAST((@latestCount-@avgAllCounts) AS DECIMAL)/CAST(@avgAllCounts AS DECIMAL))

SELECT * FROM @tblVar

输出:

19548
7107
8483

LatestCount PriorCount  PctChgLatestVsPrior PctChgLatestVsAvg
7107        8483        0                   -1
4

1 回答 1

6

您需要提供小数刻度/精度

请参阅此处:未指定精度和小数位数时的小数和数字问题

一些示例:DECIMAL(30,10) 或 DECIMAL(20)

在在线书籍http://msdn.microsoft.com/en-us/library/ms187746.aspx中查看更多信息

于 2012-05-09T20:15:15.367 回答