首页 > 解决方案 > 运行查询时遇到除以零错误

问题描述

下面是我尝试使用 Microsoft SQL Server Management Studio 运行的查询:

update [SG report MPD-4153] 
set [percent_paid] =  (try_convert(float, [Savings This Season])  * 100 / try_convert(float, [Savings 
Goal Amount]));

标签: sqlsql-serverssms

解决方案


您可以nullif()在分母上使用:

update [SG report MPD-4153] 
    set [percent_paid] =  (try_convert(float, [Savings This Season])  * 100 
                           nullif(try_convert(float, [Savings Goal Amount], 0)
                          );

推荐阅读