【问题标题】:sql - Error converting data type nvarchar to numericsql - 将数据类型 nvarchar 转换为数字时出错
【发布时间】:2015-10-28 02:05:58
【问题描述】:

这个查询工作正常

select *  from
(select meas.Inspection_No, case 
when ISNUMERIC(meas.InspectionValue) = 1 then
    cast(meas.InspectionValue as decimal(10,2))
    else 0 End as IVEKVol, meas.InspectionValue,  spec.Name, spec.   [Description], meas.LastDate, meas.ID  from
(select ism.InspectionSpecificationMeasurementID, ism.Name, ism. [Description]  from PT_InspectionSpecification [is]
inner join PT_InspectionSpecification_Measurement ism on   [is].InspectionSpecification_No = ism.InspectionSpecification_No
where [is].PartDescription = 'Anode Dispense' and   [is].InspectionSpecification_type = 'Control Plan' and ism.Name LIKE 'I-IVEKVol    Recorded%') spec
inner join 
PT_InspectionItem_Measurement  meas on  spec.InspectionSpecificationMeasurementID =   meas.InspectionSpecificationMeasurementID) data

但是当我添加 where 子句“where data.IVEKVol > 0”时,我收到错误“将数据类型 nvarchar 转换为数字时出错”

select *  from
(select meas.Inspection_No, case 
when ISNUMERIC(meas.InspectionValue) = 1 then
    cast(meas.InspectionValue as decimal(10,2))
    else 0 End as IVEKVol, meas.InspectionValue,  spec.Name, spec.   [Description], meas.LastDate, meas.ID  from
(select ism.InspectionSpecificationMeasurementID, ism.Name, ism. [Description]  from PT_InspectionSpecification [is]
inner join PT_InspectionSpecification_Measurement ism on   [is].InspectionSpecification_No = ism.InspectionSpecification_No
where [is].PartDescription = 'Anode Dispense' and   [is].InspectionSpecification_type = 'Control Plan' and ism.Name LIKE 'I-IVEKVol    Recorded%') spec
inner join 
PT_InspectionItem_Measurement  meas on  spec.InspectionSpecificationMeasurementID =   meas.InspectionSpecificationMeasurementID) data
where data.IVEKVol > 0

【问题讨论】:

    标签: sql sql-server


    【解决方案1】:

    因为data.IVEKVol列是NVARCHAR类型,所以0numberic类型

    要比较两列,DBMS 必须将两列类型转换为相同类型。

    如果data.IVEKVol 列有类似的数据:'1'、'22'、...'' 你可以试试:

    WHERE CAST(data.IVEKVol AS INT) > 0  -- Can FOAT, DOUBLE, NUMBERIC, ... instead
    

    如果不是,我认为您的代码如下:

    WHERE data.IVEKVol  > N'0' -- recommend
    -- OR
    WHERE data.IVEKVol  > '0'  -- not recommend
    

    【讨论】:

    • data.IVEKVol 不是 NVARCHAR,它被转换为小数:当 ISNUMERIC(meas.InspectionValue) = 1 then cast(meas.InspectionValue as decimal(10,2)) else 0 End as IVEKVol
    猜你喜欢
    • 2016-01-20
    • 1970-01-01
    • 2020-11-22
    • 2021-08-08
    • 1970-01-01
    • 1970-01-01
    • 2017-05-06
    • 2012-06-16
    • 2017-03-23
    相关资源
    最近更新 更多