Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Divide By Zero error when I use a case statement


Divide By Zero error when I use a case statement

Author
Message
dndaughtery
dndaughtery
SSC-Addicted
SSC-Addicted (456 reputation)SSC-Addicted (456 reputation)SSC-Addicted (456 reputation)SSC-Addicted (456 reputation)SSC-Addicted (456 reputation)SSC-Addicted (456 reputation)SSC-Addicted (456 reputation)SSC-Addicted (456 reputation)

Group: General Forum Members
Points: 456 Visits: 1071
I'm getting a divie by zero error with the following query. Can someone tell me what Im doing wrong?


SELECT ForcastUnitsTimesOMSCost, case when IsNUll(ForcastUnitsTimesOMSCost,0.00) < 0 then SUM(CTCBOHAndWCSBOHTimesOMSCost/ForcastUnitsTimesOMSCost) else 0 end as Calculation
FROM MyTable
Where (CONVERT(date, GETDATE()) BETWEEN EventStartDate AND EventEndDate)
Group By ForcastUnitsTimesOMSCost
dndaughtery
dndaughtery
SSC-Addicted
SSC-Addicted (456 reputation)SSC-Addicted (456 reputation)SSC-Addicted (456 reputation)SSC-Addicted (456 reputation)SSC-Addicted (456 reputation)SSC-Addicted (456 reputation)SSC-Addicted (456 reputation)SSC-Addicted (456 reputation)

Group: General Forum Members
Points: 456 Visits: 1071
My bad I used the wrong query:






SELECT ForcastUnitsTimesOMSCost, case when IsNUll(ForcastUnitsTimesOMSCost,0.00) <> 0 then SUM(CTCBOHAndWCSBOHTimesOMSCost/ForcastUnitsTimesOMSCost) else 0 end as Calculation
FROM MyTable
Where (CONVERT(date, GETDATE()) BETWEEN EventStartDate AND EventEndDate)
Group By ForcastUnitsTimesOMSCost


dndaughtery
dndaughtery
SSC-Addicted
SSC-Addicted (456 reputation)SSC-Addicted (456 reputation)SSC-Addicted (456 reputation)SSC-Addicted (456 reputation)SSC-Addicted (456 reputation)SSC-Addicted (456 reputation)SSC-Addicted (456 reputation)SSC-Addicted (456 reputation)

Group: General Forum Members
Points: 456 Visits: 1071
ForcastUnitsTimesOMSCost is a float datatype
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16632 Visits: 17024
You are mixing up your aggregates and scalar values. You are trying to use a case expression to control the whole column. This doesn't work at all. It is awfully hard to figure out what you are trying to do here but here is my shot in the dark.


SELECT ForcastUnitsTimesOMSCost, SUM(CTCBOHAndWCSBOHTimesOMSCost/IsNUll(ForcastUnitsTimesOMSCost, 0.00)) as Calculation
FROM MyTable
Where (CONVERT(date, GETDATE()) BETWEEN EventStartDate AND EventEndDate)
Group By ForcastUnitsTimesOMSCost
having IsNUll(ForcastUnitsTimesOMSCost, 0.00) < 0



If that doesn't help you will need to provide us a few things:

1. Sample DDL in the form of CREATE TABLE statements
2. Sample data in the form of INSERT INTO statements
3. Expected results based on the sample data

Please take a few minutes and read the first article in my signature for best practices when posting questions.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Michael Valentine Jones
Michael Valentine Jones
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3260 Visits: 11771
Try this:
SELECT
ForcastUnitsTimesOMSCost,
SUM(isnull(CTCBOHAndWCSBOHTimesOMSCost/nullif(ForcastUnitsTimesOMSCost,0.00),0.00)) as Calculation
FROM
MyTable
Where
(CONVERT(date, GETDATE()) BETWEEN EventStartDate AND EventEndDate)
Group By
ForcastUnitsTimesOMSCost


dndaughtery
dndaughtery
SSC-Addicted
SSC-Addicted (456 reputation)SSC-Addicted (456 reputation)SSC-Addicted (456 reputation)SSC-Addicted (456 reputation)SSC-Addicted (456 reputation)SSC-Addicted (456 reputation)SSC-Addicted (456 reputation)SSC-Addicted (456 reputation)

Group: General Forum Members
Points: 456 Visits: 1071
That worked, thanks
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16632 Visits: 17024
dndaughtery (10/21/2013)
That worked, thanks


Which one?

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search