在SQL Server 2012中将数据类型nvarchar转换为float时出错

时间:2016-09-07 16:53:09

标签: sql sql-server

我有以下代码

SELECT DISTINCT
    S.SCOPE
    ,convert(nvarchar, getdate(), 112) AS EFFECTIVE_START_TIME
    ,'99991231' AS EFFECTIVE_END_TIME
    ,LV.UOM_CODE
    ,'Gross Price' AS COLUMN_NAME_0
    ,CONVERT(float,LV2.VALUE_0) * CONVERT(float,LV.VALUE_1) AS VALUE_0  
    ,'Quantity' AS COLUMN_NAME_1
    ,LV.VALUE_1
FROM 
    PM_SCOPE S 
JOIN 
    PM_LOOKUP_VALUE LV ON S.SCOPE_ID = LV.SCOPE_ID
JOIN 
    PM_LOOKUP_DEFINITION LD ON LD.LOOKUP_DEFINITION_ID = LV.LOOKUP_DEFINITION_ID
JOIN 
    PM_SCOPE S2 ON S2.COMP_PRODUCT = S.COMP_PRODUCT  
JOIN 
    PM_LOOKUP_VALUE LV2 ON LV2.SCOPE_ID = S2.SCOPE_ID
JOIN 
    PM_LOOKUP_DEFINITION LD2 ON LD2.LOOKUP_DEFINITION_ID = LV2.LOOKUP_DEFINITION_ID
WHERE 
    LD.LOOKUP_NAME = 'Competitor Bundle Details' 
    AND LD2.LOOKUP_NAME = 'Competitor Gross Price List'
    AND S.GEOGRAPHY = S2.GEOGRAPHY
    AND CONVERT(VARCHAR, LV2.UPDATED_AT, 112)  > CONVERT(VARCHAR, LV.UPDATED_AT, 112) 

当我运行它时,我收到此错误

  

Msg 8114,Level 16,State 5,Line 1
  将数据类型nvarchar转换为float时出错。

然而,当我评论下面的行时,它可以正常工作

AND CONVERT(VARCHAR, LV2.UPDATED_AT, 112)  > CONVERT(VARCHAR, LV.UPDATED_AT, 112) 

我查看了数据,无法找出出现此错误的原因。

我还查看了此错误的现有帖子,但它们似乎对我没有帮助。

1 个答案:

答案 0 :(得分:0)

TRY_CONVERT(float,LV2.VALUE_0) * TRY_CONVERT(float,LV.VALUE_1) AS VALUE_0  --This will return NULL since the TRY_CONVERT results in a NULL for one of the columns, and NULL * anything is NULL
CONVERT(float,LV2.VALUE_0) * CONVERT(float,LV.VALUE_1) AS VALUE_0          --This will error out since you can't convert a nonnumeral to a FLOAT

SELECT ISNUMERIC(LV2.VALUE_0), ISNUMERIC(LV.VALUE_1)                       --This will tell you which column is causing you problems

以下是一些测试数据示例

SELECT TRY_CONVERT(float,'12.2y') * TRY_CONVERT(float,'12.25')       --This will return NULL since NULL * anything is NULL
SELECT CONVERT(float,'12.2y') * CONVERT(float,'12.25')               --This will error out since you can't convert 12.2y to a FLOAT
SELECT ISNUMERIC('12.2y'), ISNUMERIC('12.2')