?
Solved

Divide by zero error encountered

Posted on 2010-08-16
4
Medium Priority
?
1,371 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 200 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 1800 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Suggested Courses

764 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