gpt4 book ai didi

SQL 十进制值作为整数插入

转载 作者:行者123 更新时间:2023-12-02 08:15:45 25 4
gpt4 key购买 nike

我正在运行一个返回小数的查询,然后将这些小数插入到表变量中。当我查询表变量时,我得到整数。谁能明白为什么会发生这种情况以及如何纠正?使用Sql Server 2000。

DECLARE @Nov Table(custCode varchar(10), PromiseAvg decimal, ShipAvg decimal )

INSERT INTO @Nov
SELECT JM.CustomerCode
, isnull(AVG(Cast(DATEDIFF(dd, ISNULL(startDate, 0), ISNULL(PromiseDate, 0)) As Decimal)),0) As PromiseAvg
, isnull(AVG(Cast(DATEDIFF(dd, ISNULL(startDate, 0), ISNULL(BOL.ShipDate, 0)) As Decimal)),0) As ShipAvg
from jobitems JI
LEFT JOIN jobmaster JM ON JI.Job_Number = JM.JobNumber
LEFT JOIN dbo.NCL_BOLDetails BD ON JM.JobNumber = BD.JobNumber AND JI.Item_Code = BD.ItemNumber
INNER JOIN dbo.NCL_BOLs BOL ON BD.BOLID = BOL.BOLID
WHERE StartDate BETWEEN '20091101' AND '20091130'
Group By JM.CustomerCode, JM.CustLongName
Order By JM.CustomerCode

Select * from @Nov

最佳答案

您需要在表变量中定义小数的小数位数和精度。

例如

DECLARE @Nov TABLE(
custCode VARCHAR(10),
PromiseAvg decimal(18,8),
ShipAvg decimal(18,8) )

或任何适合您的比例和精度。我刚刚在 SQL Server 2008 上进行了测试,如果 create table 语句中未指定,则看起来 decimal(18,0) 是默认值。

也对您的cast进行同样的操作

... 
isnull(AVG(Cast(DATEDIFF(dd, ISNULL(startDate, 0), ISNULL(PromiseDate, 0))
As decimal(18,8))),0) As PromiseAvg,
....

关于SQL 十进制值作为整数插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4369305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com