-5

please someone help me to solve this issue: I have this select:

SELECT Cast(( Isnull(price, 0) + Isnull(notarycosts, 0)
              + Isnull(landtax, 0) + Isnull(othertaxes, 0)
              + Isnull(agentfee, 0) + Isnull(cadastralfee, 0)
              + Isnull(tabulationfee, 0)
              + Isnull(certsarcini, 0) ) / ( totalareasqm / 10000 * fixhist ) AS
                   DECIMAL(12, 4)) AS EurPerHa

Sometime i'm getting a divide by zero error and my app it's blocked until i delete the last row from the database. Could I solve somehow this issue?

Thanks!

Kermit
  • 33,827
  • 13
  • 85
  • 121
user1820705
  • 591
  • 2
  • 7
  • 17
  • 1
    possible duplicate of [How to avoid the "divide by zero" error in SQL?](http://stackoverflow.com/questions/861778/how-to-avoid-the-divide-by-zero-error-in-sql) – LittleBobbyTables - Au Revoir Dec 06 '12 at 20:38
  • 2
    divide-by-zero is coming from either totalareasqm OR fixhist = 0 so add a WHERE that checks for that in your query – n8wrl Dec 06 '12 at 20:38
  • 1
    I would try to do a simple google search of the error before posting it as a question here. – Neil Dec 06 '12 at 21:00
  • A [quick search](http://stackoverflow.com/search?q=[sql-server]+divide+by+zero+&submit=search) divide by zero) returned a bunch of threads which probably would have answered the question in a lot less time that it took to post here ;-) – Leigh Dec 06 '12 at 23:42

4 Answers4

5

Either TotalAreaSqm or FixHist is 0. As we cannot divide by zero, what should your script do when a zero exists?

I usually see approaches of either make it NULL or use a known value to make it work. I have no idea what a safe value would be.

Make it null approach

select cast((isnull(price,0)+isnull(notarycosts,0)+isnull(landtax,0)+isnull(othertaxes,0)+isnull(agentfee,0)+isnull(cadastralfee,0)+isnull(tabulationfee,0)+isnull(certsarcini,0))/(NULLIF(TotalAreaSqm, 0)/10000*NULLIF(FixHist, 0) as decimal(12,4)) as EurPerHa
billinkc
  • 59,250
  • 9
  • 102
  • 159
0

Checks FixHist and TotalAreaSqm that are not nulls and not 0.

Code:

select CASE WHEN ISNULL(NULLIF(FixHist,0),0) !=0 
                 AND ISNULL(NULLIF(TotalAreaSqm,0),0) !=0
THEN
cast((isnull(price,0)+
isnull(notarycosts,0)+
isnull(landtax,0)+
isnull(othertaxes,0)+
isnull(agentfee,0)+
isnull(cadastralfee,0)+
isnull(tabulationfee,0)+
isnull(certsarcini,0))/(TotalAreaSqm/10000*FixHist) as decimal(12,4))
ELSE 0 END as EurPerHa
Justin
  • 9,634
  • 6
  • 35
  • 47
0
SELECT IIF( TotalAreaSqm*FixHist=0
            ,null
            , cast(
                    ( isnull(price,0)
                      + isnull(notarycosts,0)
                      + isnull(landtax,0)
                      + isnull(othertaxes,0)
                      + isnull(agentfee,0)
                      + isnull(cadastralfee,0)
                      + isnull(tabulationfee,0)
                      + isnull(certsarcini,0) 
                    )
                    /
                   (TotalAreaSqm / 10000 * FixHist ) 
               as decimal(12,4))
           ) as EurPerHa
Leigh
  • 28,765
  • 10
  • 55
  • 103
gezdy
  • 3,292
  • 2
  • 14
  • 14
-3
( isnull(totalareasqm,1) / 10000 * isnull(fixhist,1) )
luchosrock
  • 712
  • 10
  • 24