记录处理sqlserver [SQL Server]在将 varchar 值 ‘16.55‘ 转换成数据类型 int 时失败的问题。

报错>

[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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值