Solved

Divide by zero error encountered

Posted on 2010-08-16
4
1,341 Views
Last Modified: 2012-05-10
Hi All,
How can avoid the following message and continue with calculations with "valid data" fields

Msg 8134, Level 16, State 1, Line 1
Divide by zero error encountered.
The statement has been terminated.

[dbo].[BILLING.E164].CALL_DURATION * CAST([dbo].[BILLING.R001.CLIENT.PLAN.L0].PRICE_SECCOND_0 AS DECIMAL(27, 5)) /
[dbo].[BILLING.E164].CALL_DURATION * CAST([dbo].[BILLING.R001.CLIENT.PLAN.L0].PRICE_SECCOND_2 AS DECIMAL(27, 5)) * 100 AS SAVING_PERCENTAGE

Thanks in Advance!
0
Comment
Question by:batman_k
4 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33444328
you can decide on what value you want when the divisor is 0:

also, try to use table aliases ...
CASE WHEN x.CALL_DURATION * CAST(l.PRICE_SECCOND_2 AS DECIMAL(27, 5)) = 0 THEN 0
ELSE 
x.CALL_DURATION * CAST(l.PRICE_SECCOND_0 AS DECIMAL(27, 5)) /
CASE WHEN x.CALL_DURATION * CAST(l.PRICE_SECCOND_2 AS DECIMAL(27, 5))= 0 THEN 1 ELSE x.CALL_DURATION * CAST(l.PRICE_SECCOND_2 AS DECIMAL(27, 5)) * 100 END AS SAVING_PERCENTAGE
...

FROM [dbo].[BILLING.E164] x
JOIN [dbo].[BILLING.R001.CLIENT.PLAN.L0] l

Open in new window

0
 
LVL 5

Assisted Solution

by:ThakurVinay
ThakurVinay earned 50 total points
ID: 33444422
there is a CASE =0 for the value which you are dividing and if it is 0 dont do that expression.

HTH
Vinay
0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 450 total points
ID: 33444469
I normally prefer the nullif, isnull route

ISNULL([dbo].[BILLING.E164].CALL_DURATION * CAST([dbo].[BILLING.R001.CLIENT.PLAN.L0].PRICE_SECCOND_0 AS DECIMAL(27, 5)) /
NULLIF([dbo].[BILLING.E164].CALL_DURATION * CAST([dbo].[BILLING.R001.CLIENT.PLAN.L0].PRICE_SECCOND_2 AS DECIMAL(27, 5)) * 100,0),0) AS SAVING_PERCENTAGE
0
 
LVL 3

Expert Comment

by:PrakashRaoBS
ID: 33444473
Try this..

if ([dbo].[BILLING.E164].CALL_DURATION > 0 and [dbo].[BILLING.R001.CLIENT.PLAN.L0].PRICE_SECCOND_2 > 0)
begin
dbo].[BILLING.E164].CALL_DURATION * CAST([dbo].[BILLING.R001.CLIENT.PLAN.L0].PRICE_SECCOND_0 AS DECIMAL(27, 5)) /
[dbo].[BILLING.E164].CALL_DURATION * CAST([dbo].[BILLING.R001.CLIENT.PLAN.L0].PRICE_SECCOND_2 AS DECIMAL(27, 5)) * 100 AS SAVING_PERCENTAGE
end
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
find SQL job run average duration 24 55
Change this SQL to get all nodes 3 39
Regarding Disk IO 3 46
Database Mail Profiles 1 21
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question