Solved

Divide by zero error encountered

Posted on 2010-08-16
4
1,309 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]
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

771 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

17 Experts available now in Live!

Get 1:1 Help Now