第二种情况将数据类型varchar转换为数字时出错

时间:2022-07-15 16:37:38

The values are stored in varchar.I try to check if the values are numeric then convert to decimal, if not then return the original value. The first case itself work fine if I comment out the second part.

值存储在varchar中。我尝试检查值是否为数字然后转换为十进制,否则返回原始值。如果我注释掉第二部分,第一个案例本身就可以正常工作。

CASE WHEN ISNUMERIC(Num) = 1 THEN CONVERT(DECIMAL(7,2),CONVERT(FLOAT, Num))
     WHEN ISNUMERIC(Num) = 0 THEN Num END,

1 个解决方案

#1


1  

The problem is that you are returning different types. You need convert the return values to the same type for safe query. Either convert the decimal back to varchar, or return 0 when num is not a number.

问题是你要返回不同的类型。您需要将返回值转换为相同类型以进行安全查询。将小数转换回varchar,或者当num不是数字时返回0。

#1


1  

The problem is that you are returning different types. You need convert the return values to the same type for safe query. Either convert the decimal back to varchar, or return 0 when num is not a number.

问题是你要返回不同的类型。您需要将返回值转换为相同类型以进行安全查询。将小数转换回varchar,或者当num不是数字时返回0。