Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Query Error "Divide by zero error encountered."

Author  Topic 

k9-961
Starting Member

6 Posts

Posted - 2011-09-29 : 09:58:05
I get good results with most queries, but on some I am getting the ERROR "Divide by zero error encountered." How can I change tyhe CASE statement to look for this? This is what I have currently:

CAST(Round(SUM(CASE
WHEN [VisitType] = 'OBS' THEN 1
ELSE 0
END) / Nullif (SUM(CASE
WHEN ( [VisitType] = 'IP'
AND [Flag] <> 'S' )
OR ( [VisitType] =
'OBS' )
THEN
1.0
ELSE 0.0
END), 4), 4) AS FLOAT) AS [Calc Field %]

Kristen
Test

22859 Posts

Posted - 2011-09-29 : 11:26:31
Easiest way is to do something like

SELECT DivideThis / NullIf(DivideByThis, 0.0)

you code appears to have

SELECT DivideThis / NullIf(DivideByThis, 4)
Go to Top of Page

Snirk
Starting Member

2 Posts

Posted - 2011-09-30 : 04:02:36
The same? as Kristen said:
CAST (
Round(
SUM(
CASE WHEN [VisitType] = 'OBS'
THEN 1
ELSE 0
END) /
Nullif (
SUM(
CASE WHEN ( [VisitType] = 'IP' AND [Flag] <> 'S' ) OR ( [VisitType] = 'OBS' )
THEN 1.0
ELSE 0.0
END
),
4), -- HERE MUST BE 0.0 !!!!
4)
AS FLOAT ) AS [Calc Field %]

You can't devide by zero, not be 4 as it appears in you script
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-30 : 04:15:02
quote:
Originally posted by Snirk

The same? as Kristen said:
CAST (
Round(
SUM(
CASE WHEN [VisitType] = 'OBS'
THEN 1
ELSE 0
END) /
Nullif (
SUM(
CASE WHEN ( [VisitType] = 'IP' AND [Flag] <> 'S' ) OR ( [VisitType] = 'OBS' )
THEN 1.0
ELSE 0.0
END
),
4), -- HERE MUST BE 0.0 !!!!
4)
AS FLOAT ) AS [Calc Field %]

You can't devide by zero, not be 4 as it appears in you script


is it an echo?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2011-09-30 : 05:51:34
An echo?
Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2011-09-30 : 08:07:54
Ok. I give up. I will start shadowing Peter and Kristen and Rob and just cut and paste their answers as my own so i can get a massive post count too

http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx
How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2011-09-30 : 12:20:59
"Snirk
Starting Member
2 Posts
"

"so i can get a massive post count too"

How "massive" were you dreaming of?

Go to Top of Page
   

- Advertisement -