SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Divide by zero error encountered problem


Divide by zero error encountered problem

Author
Message
mamidimahesh-127681
mamidimahesh-127681
Valued Member
Valued Member (74 reputation)Valued Member (74 reputation)Valued Member (74 reputation)Valued Member (74 reputation)Valued Member (74 reputation)Valued Member (74 reputation)Valued Member (74 reputation)Valued Member (74 reputation)

Group: General Forum Members
Points: 74 Visits: 15
Hi,

I am facing problem with following query.

Declare @Res int
Set @Res = 0
Select @Res = 5000/(0-0)
Select @Res as Result

If I execute the above query it gives me "Divide by zero error encountered" error . Instead of geting this error can i get Result as 0(whenever this error comes). Is there anyway to do this?

Regards,
Bobby(UserID=132570) <script language=JavaScript></script>
Dan Collier
Dan Collier
SSC Rookie
SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)

Group: General Forum Members
Points: 31 Visits: 16

This will give your desired reults without error.

Declare @Res int ,@denominator int
Set @Res = 0
Set @denominator = 0-0
Select @Res = CASE WHEN @denominator = 0 THEN 0 ELSE 5000/(@denominator) END
Select @Res as Result


mamidimahesh-127681
mamidimahesh-127681
Valued Member
Valued Member (74 reputation)Valued Member (74 reputation)Valued Member (74 reputation)Valued Member (74 reputation)Valued Member (74 reputation)Valued Member (74 reputation)Valued Member (74 reputation)Valued Member (74 reputation)

Group: General Forum Members
Points: 74 Visits: 15

Hi,

Thanks . This solves my problem. But there is another poblem with this. In my application, all sql statements will be build by my front end. In these statements "/" operator might be there or not. Sometimes "/" operator may come more than once. Please have a look of some examples which my application is generated.

IF .00 > 0
begin
Select Result = 12000.00
end
ELSE
begin
Select Result = 0
end

IF 12000.00<6500
begin
Select Result = 12000.00*0.0175
end
ELSE
begin
Select Result = 0
end

Declare @Res int
Set @Res = 0
Select @Res = 12000.00
Select @Res as Result

Declare @Res int
Set @Res = 0
Select @Res = .00/(.00-.00)
Select @Res as Result

Now u suggest me how can I proceed. If i get a error "Divide by zero error encountered" how can i make that error as "0". My schedule is geting delayed because of this problem

Please help me in this.........

Bobby


Razvan Socol
Razvan Socol
SSC Eights!
SSC Eights! (855 reputation)SSC Eights! (855 reputation)SSC Eights! (855 reputation)SSC Eights! (855 reputation)SSC Eights! (855 reputation)SSC Eights! (855 reputation)SSC Eights! (855 reputation)SSC Eights! (855 reputation)

Group: General Forum Members
Points: 855 Visits: 129

If you want cannot change the code, but you want to ignore divide by zero errors (to get null instead), you can use the ARITHABORT and ANSI_WARNINGS settings, like this:

SET ARITHABORT OFF
SET ANSI_WARNINGS OFF
SELECT 1/0 -- you will get null

You will also get a warning (but not an error). If you want to suppress the warning too, use SET ARITHIGNORE ON.

Warning: using these options may cause some other problems, including the fact that you can't use indexes on computed columns.

My favourite way to handle such errors (without changing the default settings) is:

DECLARE @a money, @b int
SELECT @a=1, @b=0
SELECT ISNULL(@a/NULLIF(@b,0),0)

Razvan





mamidimahesh-127681
mamidimahesh-127681
Valued Member
Valued Member (74 reputation)Valued Member (74 reputation)Valued Member (74 reputation)Valued Member (74 reputation)Valued Member (74 reputation)Valued Member (74 reputation)Valued Member (74 reputation)Valued Member (74 reputation)

Group: General Forum Members
Points: 74 Visits: 15

Hi Razvan,

Thanks a lot. As of now i am not using index in my database. Anyway thanks for giving me the warning. I will keep in mind.

Bobby


Bruce Gilpin
Bruce Gilpin
SSC-Enthusiastic
SSC-Enthusiastic (176 reputation)SSC-Enthusiastic (176 reputation)SSC-Enthusiastic (176 reputation)SSC-Enthusiastic (176 reputation)SSC-Enthusiastic (176 reputation)SSC-Enthusiastic (176 reputation)SSC-Enthusiastic (176 reputation)SSC-Enthusiastic (176 reputation)

Group: General Forum Members
Points: 176 Visits: 11

I'm not sure what "0-0" means. If your demonator is a variable or a computation involving variables than you can then you can do somthing like this:

delcare @res int (I assume you know that this will truncate the decimal portion of your result)

if @queryvar1 - @queryvar2 <> 0 begin

select @res = 5000 / @queryvar1 - queryvar2

else begin

select @res = 0

end


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search