将值添加到datetime列会导致溢出

时间:2013-04-26 09:41:23

标签: sql sql-server

出现此错误:

  

向'datetime'列添加值会导致溢出。

CASE U.BasePool 
            WHEN 0 THEN 'N/A'
            WHEN -1 THEN 'N/A'
            ELSE CASE 
                    WHEN SUM(SUM(B.TransactionCount)) OVER (Partition by U.ContractID) > U.BasePool THEN 'IN-OVERAGE'
                    --WHEN SUM(SUM(B.TransactionCount)) OVER (Partition by U.ContractID) + (SUM(SUM(B.TransactionCount))  OVER (Partition by U.ContractID)/MonthNum) > U.BasePool THEN DATEADD(MM, 1, GETDATE())
                    ELSE  CASE WHEN MonthNum<1 THEN NULL ELSE CONVERT(VARCHAR(20),DATEADD(MM,CAST(ROUND((U.BasePool - SUM(SUM(B.TransactionCount))  OVER (Partition by U.ContractID)) 
                                /(SUM(SUM(B.TransactionCount))  OVER (Partition by U.ContractID)/MonthNum),0) as Int), GETDATE()),101)

                    --(basepool - sumcontract) / (sumcontract/monthNum ) is the expected months to reach overage
                END 
            END
            END AS  ExpectedDate

1 个答案:

答案 0 :(得分:1)

最高datetime值为'9999-12-31',因此您可以向getdate()添加大约95840个月 这会严重地产生错误:

select dateadd(month,95841, getdate() )

在计算要添加的月数时,你一定是犯了错误。