将 varchar 中的数值转换为十进制

Converting numeric values in varchar to decimal

将数值从 varchar 数据类型转换为 decimal 时出错

select 
    case PCI1__c 
       when ISNUMERIC(rtrim(ltrim(PCI1__c))) 
          then convert(decimal(10, 2), PCI1__c)
          else null 
    end as PCI1__c 
from 
    MX_CREDIT_ANALYSIS;

Fyi,该列包含 'no revenue'、'two'、'revenue22'、'5.5'

等值

获取错误:

Conversion failed when converting the varchar value '5.5' to data type int.

isnumeric() return 是一个 integer,所以当你执行 CASE pci1__c WHEN isnumeric(...) ... 然后 pci1__c,即 varchar,得到与 integer 相比。为了能够进行比较,DBMS 想要将 varchar 向上转换为 integer。失败,因为(预期)integer 表示不包含小数点。

自 2012 年 try_convert() 可用。它会尽可能转换,否则 return NULL.

SELECT try_convert(decimal(10, 2), pci1__c) pci1__c
       FROM mx_credit_analysis;

如果你想使用CASE,那么

查询

select 
    case when ISNUMERIC(rtrim(ltrim([PCI1__c]))) = 1
    then convert(decimal(10, 2), PCI1__c)
    else null end as PCI1__c 
from [MX_CREDIT_ANALYSIS];