September 20, 2017 at 10:22 am
Hi friends,
I get the below error from this sqlselect (serno * 100) / (100 - bargain) from yachtm
Msg 8134, Level 16, State 1, Line 1
Divide by zero error encountered.
Sample data from yachtm table..
select serno,bargain from yachtm
SERNO BARGAIN
7.90 0.0000
8.67 0.0000
0.00 100.0000
please give your thoughts.. thank you all so much
September 20, 2017 at 11:03 am
newbieuser - Wednesday, September 20, 2017 10:22 AMHi friends,I get the below error from this sql
select (serno * 100) / (100 - bargain) from yachtm
Msg 8134, Level 16, State 1, Line 1
Divide by zero error encountered.Sample data from yachtm table..
select serno,bargain from yachtm
SERNO BARGAIN
7.90 0.0000
8.67 0.0000
0.00 100.0000please give your thoughts.. thank you all so much
My thoughts are that the error message is 100% accurate. But what is your question?
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
September 20, 2017 at 11:16 am
Phil Parkin - Wednesday, September 20, 2017 11:03 AMMy thoughts are that the error message is 100% accurate. But what is your question?
I agree with Phil. 0 / 0 = [Insert infinite options here] = SQL server error is correct. 🙂
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk
September 20, 2017 at 11:22 am
Hi friends,
I understand when bargain=100.0000, I get the divide by zero error. But when bargain =0.0000 and serno=7.90, the result should be 7.9 and I need to use the same logic (serno * 100) / (100 - bargain) for all the records..
Please help..
Thanks a lot again
September 20, 2017 at 11:25 am
newbieuser - Wednesday, September 20, 2017 11:22 AMHi friends,I understand when bargain=100.0000, I get the divide by zero error. But when bargain =0.0000 and serno=7.90, the result should be 7.9 and I need to use the same logic (serno * 100) / (100 - bargain) for all the records..
Please help..Thanks a lot again
What should be the result of the divide by 0?
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk
September 20, 2017 at 11:31 am
Is there a way to exclude those records that divide by zero in the SQL and do the calculation only for those records that is not zero in the divide by clause.. Thanks
September 20, 2017 at 11:42 am
newbieuser - Wednesday, September 20, 2017 11:31 AMIs there a way to exclude those records that divide by zero in the SQL and do the calculation only for those records that is not zero in the divide by clause.. Thanks
You would use a where clause to exclude records. In your case add:
WHERE serno > 0
after the from clause.
Sue
Edit: - Oops, didn't pay enough attention to the table and mixed up you columns. See responses below.
September 20, 2017 at 11:43 am
Use a case statement to check whether bargain = 100 and only do the division if it's not.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
September 20, 2017 at 11:43 am
newbieuser - Wednesday, September 20, 2017 11:31 AMIs there a way to exclude those records that divide by zero in the SQL and do the calculation only for those records that is not zero in the divide by clause.. Thanks
Of course, add
WHERE BARGAIN <> 100
to the bottom of your query.
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
September 20, 2017 at 12:16 pm
Thanks a lot everyone
September 20, 2017 at 12:55 pm
There is a third option. I don't have the time right now to do a comparison.
select (serno * 100) / NULLIF(100 - bargain, 0) from yachtm
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply