plsql中除数等于0是什么?

问题描述 投票:0回答:3
SELECT pstartdate, 
       opbal, 
       joined, 
       resign, 
       ( opbal + joined - resign ) clbal 
       , 
       ( Round(( ( resign * 100 ) / ( opbal + joined ) ) / 100, 2) * 100 
       ) attriation 
FROM   (SELECT pstartdate, 
               penddate, 
               Getopempbal(pstartdate)         OpBal, 
               Getempjn(pstartdate, penddate)  Joined, 
               Getempres(pstartdate, penddate) Resign 
        FROM   (SELECT Add_months(:startdate, LEVEL - 1) pstartdate, 
                       Add_months(:startdate, LEVEL) - 1 penddate 
                FROM   dual 
                CONNECT BY LEVEL <= Months_between( :enddate, :startdate ) + 1)) 
ORDER  BY To_number(1) 

当我执行此查询时,出现错误

除数等于零。

我认为这个地方有错误

(round(((resign*100)/(opbal+joined))/100,2)*100) attriation

sql plsql oracle11g
3个回答
3
投票

我建议您在做除法时使用

nullif()
函数:

select . . .
       ( Round(( ( resign * 100 ) / nullif( opbal + joined, 0 ) ) / 100, 2) * 100 
       ) attriation 

如果分母为零,则返回

NULL


0
投票

是否有可能返回零而不是空值?


-2
投票
SELECT pstartdate,
       opbal,
       joined,
       resign,
       (opbal+joined-resign) clbal,
       round((**NULLIF**(resign,0) *100)/(opbal + joined)) ab
FROM
  (SELECT pstartdate,
          penddate,
          getopempbal(pstartdate) opbal,
          getempjn(pstartdate,penddate) joined,
          getempres(pstartdate,penddate) resign
   FROM
     (SELECT add_months(:startdate, LEVEL-1) pstartdate,
             add_months(:startdate, LEVEL)-1 penddate
      FROM dual CONNECT BY LEVEL <= months_between(:enddate, :startdate) + 1))
ORDER BY to_number(1)
© www.soinside.com 2019 - 2024. All rights reserved.