[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

rodchar

3/20/2007 6:29:00 PM

hey all,
i have a stored procedure that takes 2 variables and divides them. what's
the best method to handle the exception of dividing by zero?

thanks,
rodchar
8 Answers

sloan

3/20/2007 6:42:00 PM

0


I usually do this:

HOWEVER, you need to watch for inputs, outputs , rounding, and
if a N/0 is ok to return a "0".


-- CREATE FUNCTION
ALTER FUNCTION dbo.udfSafeDivision (@v1 decimal ( 10 , 2 ) , @v2 decimal (
10 , 2 ) )

RETURNS
decimal ( 10 , 2 )

AS

BEGIN

declare @returnValue decimal ( 10 , 2 )

if (@v2=0) -- No Division by Zero
BEGIN
select @returnValue = 0
END
else
BEGIN
select @returnValue = convert(decimal ( 10 , 2 ) , (convert(decimal
( 10 , 2 ) , @v1) / convert(decimal ( 10 , 2 ) , @v2) ) )
END





return @returnValue


END



"rodchar" <rodchar@discussions.microsoft.com> wrote in message
news:A6FBFE9D-A8D7-43BE-A1C8-341E172C1165@microsoft.com...
> hey all,
> i have a stored procedure that takes 2 variables and divides them. what's
> the best method to handle the exception of dividing by zero?
>
> thanks,
> rodchar


Aaron [SQL Server MVP]

3/20/2007 6:51:00 PM

0

What do you want as the result? NULL?

DECLARE @numerator INT, @denominator INT;
SET @numerator = 5;
SET @denominator = 0;
SELECT @numerator / NULLIF(@denominator, 0);

Zero?

DECLARE @numerator INT, @denominator INT;
SET @numerator = 5;
SET @denominator = 0;
SELECT CASE WHEN @denominator = 0 THEN 0 ELSE @numerator / @denominator END;

The numerator?

DECLARE @numerator INT, @denominator INT;
SET @numerator = 5;
SET @denominator = 0;
SELECT @numerator / COALESCE(NULLIF(@denominator, 0), 1);

--
Aaron Bertrand
SQL Server MVP
http://www.sq...
http://www.aspfa...






"rodchar" <rodchar@discussions.microsoft.com> wrote in message
news:A6FBFE9D-A8D7-43BE-A1C8-341E172C1165@microsoft.com...
> hey all,
> i have a stored procedure that takes 2 variables and divides them. what's
> the best method to handle the exception of dividing by zero?
>
> thanks,
> rodchar


Plamen Ratchev

3/20/2007 7:18:00 PM

0

I have normally used this short syntax for @x / @y:

ISNULL(@x / NULLIF(@y, 0), n)

Then set n based on what your result should be (n=0 returns 0, n=@x returns
@x, etc.).

HTH,

Plamen Ratchev
http://www.SQL...


Gert-Jan Strik

3/20/2007 7:29:00 PM

0

Wow, that is a lot of code for such a simple task...

rodchar,

One of the simples solutions is to do something like this:

SELECT @v1 / NULLIF( @v2, 0)

This will return NULL if division was invalid (Division by zero) or else
it will give the proper result.

If you prefer the result of an invalid division to be zero, you can add
a COALESCE:

SELECT COALESCE( @v1 / NULLIF(@v2,0), 0)

HTH,
Gert-Jan



sloan wrote:
>
> I usually do this:
>
> HOWEVER, you need to watch for inputs, outputs , rounding, and
> if a N/0 is ok to return a "0".
>
> -- CREATE FUNCTION
> ALTER FUNCTION dbo.udfSafeDivision (@v1 decimal ( 10 , 2 ) , @v2 decimal (
> 10 , 2 ) )
>
> RETURNS
> decimal ( 10 , 2 )
>
> AS
>
> BEGIN
>
> declare @returnValue decimal ( 10 , 2 )
>
> if (@v2=0) -- No Division by Zero
> BEGIN
> select @returnValue = 0
> END
> else
> BEGIN
> select @returnValue = convert(decimal ( 10 , 2 ) , (convert(decimal
> ( 10 , 2 ) , @v1) / convert(decimal ( 10 , 2 ) , @v2) ) )
> END
>
> return @returnValue
>
> END
>
> "rodchar" <rodchar@discussions.microsoft.com> wrote in message
> news:A6FBFE9D-A8D7-43BE-A1C8-341E172C1165@microsoft.com...
> > hey all,
> > i have a stored procedure that takes 2 variables and divides them. what's
> > the best method to handle the exception of dividing by zero?
> >
> > thanks,
> > rodchar

rodchar

3/20/2007 7:47:00 PM

0

Thanks everyone for the generous replies, I also looked into the following
BEGIN TRY
END TRY
BEGIN CATCH
END CATCH

Works too.


"rodchar" wrote:

> hey all,
> i have a stored procedure that takes 2 variables and divides them. what's
> the best method to handle the exception of dividing by zero?
>
> thanks,
> rodchar

Aaron [SQL Server MVP]

3/20/2007 7:56:00 PM

0

> Thanks everyone for the generous replies, I also looked into the following
> BEGIN TRY
> END TRY
> BEGIN CATCH
> END CATCH
>
> Works too.
>

Is that kind of like actually going off a cliff just to make sure your
little sister straps her seatbelt on? What are you going to do here, not
return a resultset because one of the rows might have yielded a division by
zero error? I bet the app programmers will love dealing with that one...

--
Aaron Bertrand
SQL Server MVP
http://www.sq...
http://www.aspfa...



rodchar

3/20/2007 8:43:00 PM

0

point taken.

"Aaron Bertrand [SQL Server MVP]" wrote:

> > Thanks everyone for the generous replies, I also looked into the following
> > BEGIN TRY
> > END TRY
> > BEGIN CATCH
> > END CATCH
> >
> > Works too.
> >
>
> Is that kind of like actually going off a cliff just to make sure your
> little sister straps her seatbelt on? What are you going to do here, not
> return a resultset because one of the rows might have yielded a division by
> zero error? I bet the app programmers will love dealing with that one...
>
> --
> Aaron Bertrand
> SQL Server MVP
> http://www.sq...
> http://www.aspfa...
>
>
>
>

jeevan

3/21/2007 1:22:00 AM

0


"Aaron Bertrand [SQL Server MVP]" <ten.xoc@dnartreb.noraa> wrote in message
news:urKjGnyaHHA.5108@TK2MSFTNGP03.phx.gbl...
> Is that kind of like actually going off a cliff just to make sure your
> little sister straps her seatbelt on?

You've been hanging out with Joe too long... you're starting to come up with
Celkoisms :)