dbTalk Databases Forums  

Re: Division by Zero

microsoft.public.sqlserver.dts microsoft.public.sqlserver.dts


Discuss Re: Division by Zero in the microsoft.public.sqlserver.dts forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Allan Mitchell
 
Posts: n/a

Default Re: Division by Zero - 07-11-2003 , 02:51 AM






try adapting this

declare @a int
declare @b int

set @a = 0
set @b = 1

if (@a = 0 OR @b = 0)
begin
--raiserror('Oops one of the vals is 0',16,1)
select 0 as result
End
Else
select @b /@a

--


Allan Mitchell (Microsoft SQL Server MVP)
MCSE,MCDBA
www.SQLDTS.com
I support PASS - the definitive, global community
for SQL Server professionals - http://www.sqlpass.org

<Shane> wrote

Quote:
Hi everybody - I have a problem with a division by zero error message.
Here is some code from a Stored procedure

-----------------------------------
BEGIN
Select @Deleted = Count(Policy_Ref)
from Cashbook where
Policy_Type in ('PC','TW','CV','MC','MR')
and (Record_Type = 'Deleted Trans.')
END

BEGIN
Select @PolCount = Count(Policy_Ref) from Cashbook
WHERE
Policy_Type in ('PC','TW','CV','MC','MR')
and Record_Type = 'New Transactions'
END

SELECT (@PolCount - @Deleted) as PolicyCount, (@LEPolCount -
@LEDeleted) as LEPolicyCount,

convert(numeric,(@LEPolCount - @LEDeleted))/convert(numeric,(@PolCount
- @Deleted)) as Penetration
-----------------------------------

The problem that I have is that either @PolCount OR @LEPolCount may be
zero. This means that I get an error message in SQL Analyser as follows:

Server: Msg 8134, Level 16, State 1, Procedure USER_Penetrations, Line
164
Divide by zero error encountered.

Is there a way to check to see if either value is 0 and then to return a
"friendly" result rather than an error message? By "friendly result", a
zero would be nice.

I know that I can set the variable values to zero and return the values
as zero. However, you will note that I am trying to perform a division
within the Stored Procedure and it is this that is resulting in the
error message. I can perform the calculation outside of the procedure
but I'd like to keep it within it if possible.

Thanks you for your time and consideration.

Shane

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.