SQL Server 2008: Error converting data type nvarchar to float

前端 未结 3 606
清酒与你
清酒与你 2020-11-30 15:25

Presently troubleshooting a problem where running this SQL query:

UPDATE tblBenchmarkData 
SET OriginalValue = DataValue, OriginalUnitID = DataUnitID, 
    D         


        
3条回答
  •  再見小時候
    2020-11-30 15:57

    So starting with your update query that's giving an error (please forgive me for rewriting it for my own clarity):

    UPDATE B
    SET
       OriginalValue = DataValue,
       OriginalUnitID = DataUnitID,
       DataValue = CAST(DataValue AS float) * 1.335
    FROM
       dbo.tblBenchmarkData B
       INNER JOIN dbo.tblZEGCode Z
          ON B.ZEGCodeID = Z.ZEGCodeID
    WHERE
       B.FieldDataSetID = '6956beeb-a1e7-47f2-96db-0044746ad6d5'
       AND (
          Z.ZEGCode = 'C004' OR 
          Z.ZEGParentCode LIKE 'C004%'
       )
    

    I think you'll find that a SELECT statement with exactly the same expressions will give the same error:

    SELECT
       OriginalValue,
       DataValue NewOriginalValue,
       OriginalUnitID,
       DataUnitID OriginalUnitID,
       DataValue,
       CAST(DataValue AS float) * 1.335 NewDataValue
    FROM
       dbo.tblBenchmarkData B
       INNER JOIN dbo.tblZEGCode Z
          ON B.ZEGCodeID = Z.ZEGCodeID
    WHERE
       B.FieldDataSetID = '6956beeb-a1e7-47f2-96db-0044746ad6d5'
       AND (
          Z.ZEGCode = 'C004' OR 
          Z.ZEGParentCode LIKE 'C004%'
       )
    

    This should show you the rows that can't convert:

    SELECT
        B.*
     FROM
        dbo.tblBenchmarkData B
        INNER JOIN dbo.tblZEGCode Z
           ON B.ZEGCodeID = Z.ZEGCodeID
     WHERE
        B.FieldDataSetID = '6956beeb-a1e7-47f2-96db-0044746ad6d5'
        AND (
           Z.ZEGCode = 'C004' OR 
           Z.ZEGParentCode LIKE 'C004%'
        )
        AND IsNumeric(DataValue) = 0
        -- AND IsNumeric(DataValue + 'E0') = 0 -- try this if the prior doesn't work
    

    The trick in the last commented line is to tack on things to the string to force only valid numbers to be numeric. For example, if you wanted only integers, IsNumeric(DataValue + '.0E0') = 0 would show you those that aren't.

提交回复
热议问题