0

I have a query where I need to get a certain number divided by 2 fields in 2 columns.

shelve.total_qty/(GREATEST(NVL(y.FORECAST_QUANTITY, 0), NVL(z.sales, 0))/7) as Shelve_DOC

I get the notorious error.

ORA-01476: divisor is equal to zero 01476. 00000 - "divisor is equal to zero" *Cause:
*Action:

I have read around I need a CASE/IF but I'm not sure how to..

Any help would be appreciated.

Nomad
  • 250
  • 3
  • 11
  • 27
  • There is a neat way using nullif http://stackoverflow.com/questions/861778/how-to-avoid-the-divide-by-zero-error-in-sql – edgar Nov 27 '13 at 19:45

2 Answers2

1
SELECT 
     CASE WHEN (GREATEST(NVL(y.FORECAST_QUANTITY, 0), NVL(z.sales, 0))/7) = 0 THEN null
          ELSE shelve.total_qty/(GREATEST(NVL(y.FORECAST_QUANTITY, 0), NVL(z.sales, 0))/7)
     END Shelve_DOC
  FROM ...
 WHERE ....

Should do the trick.

DCookie
  • 42,630
  • 11
  • 83
  • 92
1

Something like this?

CASE WHEN NVL(y.FORECAST_QUANTITY,0) <= 0 AND NVL(z.sales,0) <= 0
     THEN NULL
     ELSE shelve.total_qty/(GREATEST(NVL(y.FORECAST_QUANTITY, 0), NVL(z.sales, 0))/7)
     END AS Shelve_DOC
MT0
  • 143,790
  • 11
  • 59
  • 117