显示将varchar的错误转换为数字

时间:2017-11-17 15:12:31

标签: sql sql-server sql-server-2008 asp.net-mvc-4 stored-procedures

我收到此错误

  

Msg 8114,Level 16,State 5,Line 13
  将数据类型varchar转换为数字时出错。

这是我的代码:

SELECT DISTINCT  
                      DI.DealName AS 'Deal Name',  
    DI.OfferingType AS 'Offering Type',   
    DI.Date AS 'Date',  
    DI.ClassName AS 'Class',  
    ISNULL(Bench,'-') AS 'BM',  
    ISNULL(Guidance,'-') AS 'Guid',  
    DI.Industry AS 'Industry',  
    CAST( (ISNULL(DI.AverageLife,0)) AS VARCHAR(20)) AS 'WAL',CAST(  ISNULL(DI.ClassSize,0) AS VARCHAR(20)) AS 'Balance',CAST(   REPLACE(ISNULL(DI.Coupon,0),'%','') AS VARCHAR(20)) AS 'Coupon',  
       CAST(  CASE WHEN CAST(REPLACE(ISNULL(DI.PX2,0),'%','') AS DECIMAL(18,5)) = 0.00 THEN (CASE WHEN ISNULL(CAST(DI.Yield * 100 AS DECIMAL(18,3)),0) = 0 THEN (CASE WHEN (ISNULL(DI.Coupon,0)+'cpn' = 'cpn' OR ISNULL(DI.Coupon,0)+'cpn' = '0cpn') THEN '-' ELSE (ISNULL(DI.Coupon,0)+'cpn')END)ELSE CAST(ISNULL(CAST(DI.Yield * 100 AS DECIMAL(18,3)),0) AS VARCHAR(15))+'%yld' END) ELSE (ISNULL(DI.PX,0)) END AS VARCHAR(20)) AS 'Spread',  CAST(  ISNULL(CAST(DI.Yield * 100 AS DECIMAL(18,3)),0) AS VARCHAR(20)) AS 'Yield', CAST(  CASE WHEN LTRIM(RTRIM(DI.Dollar_Px)) = '' THEN 0.00 ELSE CAST(ISNULL(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(LTRIM(RTRIM(Dollar_Px)),CHAR(10),''),CHAR(13),''),'-',''),'/',''),' ',''),'%cpn',''),0) AS DECIMAL(18,5)) END AS VARCHAR(20)) AS 'Dollar_Px',  ISNULL(Notes,'') AS Notes,                 
           CAST((FRM.Rating + '/' + KBRARM.Rating) AS VARCHAR(15)) AS 'Rating'  FROM DI_DealIssuancePost DI  
         INNER JOIN DI_CompanyRatingMapping MRM ON MRM.CompanyRatingMappingID = DI.Mdy                   INNER JOIN DI_CompanyRatingMapping SPRM ON SPRM.CompanyRatingMappingID = DI.S_P  
         INNER JOIN DI_CompanyRatingMapping FRM ON FRM.CompanyRatingMappingID = DI.Fitch  
         INNER JOIN DI_CompanyRatingMapping DBRSRM ON DBRSRM.CompanyRatingMappingID = DI.DBRS                 INNER JOIN DI_CompanyRatingMapping KBRARM ON KBRARM.CompanyRatingMappingID = DI.KBRA  

         INNER JOIN DI_WeeklySectors DIWeekly ON DI.Industry = DIWeekly.Sector  

        WHERE DI.IsDeleted = 0              
          AND MRM.IsDeleted = 0  
          AND SPRM.IsDeleted = 0  
          AND FRM.IsDeleted = 0  
          AND DBRSRM.IsDeleted = 0  
          AND KBRARM.IsDeleted = 0  
          AND DI.Date IS NOT NULL   
          AND (CONVERT(DATETIME,DI.Date ,101) >= CONVERT(DATETIME,@StartDate,101))   
          AND (CONVERT(DATETIME,DI.Date ,101) <= CONVERT(DATETIME,@EndDate,101)) AND DI.CurrencyID IN (SELECT * FROM CsvToTable(@Currency)) ORDER BY DI.Date desc

Msg 8114,Level 16,State 5,Line 13 将数据类型varchar转换为数字时出错。

我可以发现这是一个错误

 CAST(  CASE WHEN CAST(REPLACE(ISNULL(DI.PX2,0),'%','') AS DECIMAL(18,5)) = 0.00 THEN (CASE WHEN ISNULL(CAST(DI.Yield * 100 AS DECIMAL(18,3)),0) = 0 THEN (CASE WHEN (ISNULL(DI.Coupon,0)+'cpn' = 'cpn' OR ISNULL(DI.Coupon,0)+'cpn' = '0cpn') THEN '-' ELSE (ISNULL(DI.Coupon,0)+'cpn')END)ELSE CAST(ISNULL(CAST(DI.Yield * 100 AS DECIMAL(18,3)),0) AS VARCHAR(15))+'%yld' END) ELSE (ISNULL(DI.PX,0)) END AS VARCHAR(20)) AS 'Spread',  CAST(  ISNULL(CAST(DI.Yield * 100 AS DECIMAL(18,3)),0) AS VARCHAR(20)) AS 'Yield', CAST(  CASE WHEN LTRIM(RTRIM(DI.Dollar_Px)) = '' THEN 0.00 ELSE CAST(ISNULL(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(LTRIM(RTRIM(Dollar_Px)),CHAR(10),''),CHAR(13),''),'-',''),'/',''),' ',''),'%cpn',''),0) AS DECIMAL(18,5)) END AS VARCHAR(20)) AS 'Dollar_Px',  ISNULL(Notes,'') AS Notes,

有人帮助解决此错误

2 个答案:

答案 0 :(得分:0)

逐一检查以下内容:

SELECT ISNUMERIC(ISNULL(DI.PX2,0)) FROM DI WHERE ISNUMERIC(ISNULL(DI.PX2,0)) = 0

SELECT ISNUMERIC(ISNULL(DI.Yield,0)) FROM DI WHERE ISNUMERIC(ISNULL(DI.Yield,0)) = 0

SELECT ISNUMERIC(ISNULL(Dollar_Px,0)) FROM TABLE WHERE ISNUMERIC(ISNULL(Dollar_Px,0)) = 0

必须存在任何无法转换为varchar

decimal字符串

答案 1 :(得分:0)

这是一个错误:

(ISNULL(DI.Coupon,0)+''cpn'' = ''cpn'' OR ISNULL(DI.Coupon,0)+''cpn'' = ''0c

pn'') THEN

您的优惠券是varchar,但如果是NULL则使用0代替它。 您应该在'0'

中使用'0cpn'作为字符串,而不是整数

所以它应该是

(ISNULL(DI.Coupon,''0'')+''cpn'' = ''cpn'' OR ISNULL(DI.Coupon,''0'')+''cpn'' = ''0c

pn'') THEN