报错>
[22018] [Microsoft][SQL Server Native Client 10.0][SQL Server]在将 varchar 值 '16.55' 转换成数据类型 int 时失败。 (245)
cast( case when patindex('%[^0-9|.|+]%',ltrim(rtrim(opelngm))) != 0
or opelngm is null
or opelngm = '' then 0
else ltrim(rtrim(opelngm))
end
as decimal(18,10))/3600
问题就发生在 then 0 这里,我猜测(来不及验证了,能用就行了哈哈哈)sqlserver在进行数据转换的时候,整型或者浮点型数据谁先进入到转换任务中,就把该字段数据统一定为那个类型。也就是说我的数据,先遇到了不满足条件的了,then进入计算,返回0,在进行转换的时候现将then 返回的0进行了int转换,所以后边的小数点的16.55就转不成int了,也就报错了。
解决办法
cast( case when patindex('%[^0-9|.|+]%',ltrim(rtrim(opelngm))) != 0
or opelngm is null
or opelngm = '' then cast(0 as decimal(18,10))
else ltrim(rtrim(opelngm))
end
as decimal(18,10))/3600
让返回的值再转一次decimal,我也试过直接返回0.0,0.00了,不行,emo