这里如何避免零除错误?

时间:2012-07-05 02:39:33

标签: sql sql-server sql-server-2008 tsql divide-by-zero

我遇到的以下查询遇到除以0错误:

select  pp.building_name, 
        ld.tenant_trading_name,  
        tenancy_reference,  
        ld.turnover_threshold, 
        ld.percentage_rent, 
        ld.income_base_rent_ach, 
        ld.income_base_rent_ach / ld.percentage_rent as correct 
from 
    lease_deal.lease ld

inner join property.property pp
    on ld.building_id = pp.building_id

where 
    (ld.income_base_rent_ach / ld.percentage_rent ) <> ld.turnover_threshold
    and lease_status = 'APPROVED'
    and ld.progenesis_load_date is  null
order by pp.building_name

我尝试通过执行以下操作来纠正此问题 - 但我收到语法错误,我不确定为什么?这里的语法错误是什么?

select  pp.building_name, 
        ld.tenant_trading_name,  
        tenancy_reference,  
        ld.turnover_threshold, 
        ld.percentage_rent, 
        ld.income_base_rent_ach, 
        ld.income_base_rent_ach / ld.percentage_rent as correct 
from 
    lease_deal.lease ld

inner join property.property pp
    on ld.building_id = pp.building_id

where 
    case when ld.percentage_rent = 0
    then 1=1
    else ((ld.income_base_rent_ach / ld.percentage_rent ) <> ld.turnover_threshold)
    end
    and lease_status = 'APPROVED'
    and ld.progenesis_load_date is  null
order by pp.building_name

1 个答案:

答案 0 :(得分:3)

如何更换

(ld.income_base_rent_ach / ld.percentage_rent ) <> ld.turnover_threshold

ld.income_base_rent_ach <> (ld.percentage_rent * ld.turnover_threshold)