首页 > 解决方案 > SQL Server 转换在不同服务器上失败

问题描述

我有一个 select 语句,它的Isnumeric转换在一台服务器上失败并在另一台服务器上运行良好。

两台服务器都使用 SQL Server 2012。数据库是第二台服务器上的副本。

错误:

将 varchar 值“700.00”转换为数据类型 int 时转换失败

主选择实际上不返回任何行。

voidOrDenied = (case when isnumeric(c.reasonpending) = 1 and 
                      cast(left(c.reasonpending, charindex(',', c.reasonpending+',')-1) as int)
                        between 1 and 25 then 'Void'
                      when c.reasonpending like '%void%' then 'Void'
                      else 'Denied' end),

第二台服务器上可能有什么不同?

标签: sqlsql-server

解决方案


使用try_cast()

(case when try_cast(left(c.reasonpending, charindex(',', c.reasonpending+',')-1) as int) between 1 and 25
      then 'Void'
      when c.reasonpending like '%void%' then 'Void'
      else 'Denied'
end)

推荐阅读