执行以下查询时获取错误消息

时间:2018-02-28 22:25:17

标签: sql sql-server

SELECT o.TypeAbv + ' (' + o.TypeCode + ')' AS 'Type', o.PracticeAbv + ' (' + o.PracticeCode + ')' 
AS 'Practice', b.Unit AS 'Unit #', o.Acres AS 'Acres', o.InsuredSharePct AS 'Share', o.ApprovedYield 
AS 'Approved Yield', o.AdjustedYield AS 'Adjusted Yield', o.ProdGuarantee AS 'Guar/Acre', o.RateYield 
AS 'Simple Avg Yield', 
(CAST((CASE WHEN o.ProdGuarantee = '' THEN Null ELSE o.ProdGuarantee END) as decimal(10,2)) * CAST(o.Acres as decimal(10,2))) 
AS 'Total Guar UOM', o.BasePrice AS 'Price', o.Liability AS 'Total Protection', o.UnitDiscountFactor 
AS 'Unit Disc Factor', o.TotalPremium AS 'Base Premium', o.ProdPremium AS 'Subsidy Premium', o.Subsidy 
AS 'Subsidy Amount', o.SubsidyPercent AS 'Subsidy %', o.PremiumRate AS 'Base Prem Rate' 
FROM RMA_Output o INNER JOIN AR_Premium_Baseline b ON b.TestCaseID = o.TestCaseID 
AND b.ModuleNumber = o.ModuleNumber AND b.CropLineNumber = o.CropLineNumber AND b.ARLineNumber = o.ARLineNumber 
WHERE o.TestCaseID = 'AR_CA_Sacmnto_Chry_ARH_FeeByGrp_OuBuElection_MP4006' AND o.ModuleNumber = 4 AND o.CropLineNumber = 1 
AND o.CropYear = 2018
ORDER BY b.Unit, o.PracticeCode, o.TypeAbv
  

错误消息:消息8114,级别16,状态5,行32错误将数据类型nvarchar转换为数字。

有人有解决方法吗?

1 个答案:

答案 0 :(得分:0)

看起来这是您进行转换的唯一一条线。

 ,(CAST((CASE WHEN o.ProdGuarantee = '' THEN Null ELSE o.ProdGuarantee END) as decimal(10,2)) * CAST(o.Acres as decimal(10,2))) AS 'Total Guar UOM'

如果是2012+,请使用 TRY_CONVERT()

 ,try_convert(decimal(10,2),o.ProdGuarantee) * try_convert(decimal(10,2),o.Acres) AS 'Total Guar UOM'

如果转换失败,Try_Convert()将返回NULL,并且不会抛出错误。