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


Fixing Divide by Zero Error


Fixing Divide by Zero Error

Author
Message
VegasL
VegasL
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: 159
Hello,

I have in select statement select t1.A/t1.B but get divide by zero error

when I used with isnull isnull(t1.A/t1.B,'-')


I get error Error converting data type varchar to float.

how to fix?

Thanks
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (52K reputation)SSC Guru (52K reputation)SSC Guru (52K reputation)SSC Guru (52K reputation)SSC Guru (52K reputation)SSC Guru (52K reputation)SSC Guru (52K reputation)SSC Guru (52K reputation)

Group: General Forum Members
Points: 52432 Visits: 38684
VegasL (4/15/2013)
Hello,

I have in select statement select t1.A/t1.B but get divide by zero error

when I used with isnull isnull(t1.A/t1.B,'-')


I get error Error converting data type varchar to float.

how to fix?

Thanks


select t1.A / nullif(t1.B,0)

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
kapil_kk
kapil_kk
Hall of Fame
Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)

Group: General Forum Members
Points: 3644 Visits: 2766
VegasL (4/15/2013)
Hello,

I have in select statement select t1.A/t1.B but get divide by zero error

when I used with isnull isnull(t1.A/t1.B,'-')


I get error Error converting data type varchar to float.

how to fix?

Thanks

You can handle this using CASE also:
SELECT CASE when t1.b =0 THEN 0 ELSE t1.A/t1.B

_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
ALZDBA
ALZDBA
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16190 Visits: 8971
why are you storing numeric values in a varchar column. As experienced, this will get you in troubles.
( in most cases because of formatting issues or just because someone put non-numeric data in it)

There must be a zero ( 0 ) in your t1.B column.
add a case statement to check it so it doesn't produce the error.

select t1.a / case t1.b when 0 then NULL /* to the result will be null */
else t1.B
end as TheResult



Johan


Dont drive faster than your guardian angel can fly ...
but keeping both feet on the ground wont get you anywhere w00t

- How to post Performance Problems
- How to post data/code to get the best help


- How to prevent a sore throat after hours of presenting ppt ?


press F1 for solution, press shift+F1 for urgent solution :-D


Need a bit of Powershell? How about this

Who am I ? Sometimes this is me Alien but most of the time this is me Hehe
VegasL
VegasL
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: 159
Thanks Lynn, you'res was the easiest solution for me. I'll have to look into the others more. Thanks All!
EdgeOfKnowWare
EdgeOfKnowWare
SSC-Enthusiastic
SSC-Enthusiastic (140 reputation)SSC-Enthusiastic (140 reputation)SSC-Enthusiastic (140 reputation)SSC-Enthusiastic (140 reputation)SSC-Enthusiastic (140 reputation)SSC-Enthusiastic (140 reputation)SSC-Enthusiastic (140 reputation)SSC-Enthusiastic (140 reputation)

Group: General Forum Members
Points: 140 Visits: 291
I would agree with Lynn's post. You can not divide by zero, but if you divide by NULL, you'll get NULL.


DECLARE @i FLOAT = 10, @x FLOAT = 0
SELECT @i / NULLIF(@x,0)


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