Solved

Divide by zero error encountered

Posted on 2010-08-16
4
1,323 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 142

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

867 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now