Avoid Divide By Zero Error Encountered. Sql Server
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting divide by zero error encountered in sql server 2012 ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the
Divide By Zero Error Encountered Sql Server 2008
Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a
Divide By Zero Error Encountered Excel
minute: Sign up Simple way to prevent a Divide By Zero error in SQL up vote 9 down vote favorite 2 I have a SQL query which used to cause a Divide By Zero exception, I've wrapped it in a CASE
Msg 8134 Level 16 State 1 Line 1 Divide By Zero Error Encountered
statement to stop this from happening. Is there a simpler way of doing this? Here's my code: Percentage = CASE WHEN AttTotal <> 0 THEN (ClubTotal/AttTotal) * 100 ELSE 0 END sql sql-server-2008 tsql sql-server-2005 share|improve this question asked Oct 28 '13 at 9:22 Denys Wessels 10.5k43872 1 It depends on how you wan't your query and result to behave when AttTotal is 0 or NULL, so there is no general way of doing it. I'd say your query is just fine. oracle sql divide by zero Even if you write your query differently using ISNULL or NULLIF at the end it will likely execute the same. –Nenad Zivkovic Oct 28 '13 at 9:34 As has been pointed out by others, the logic in this CASE statement doesn't quite make sense to us. See my comment on my answer for a brief explanation and then choose whether you want your original answer or mine. My answer would be a more usual solution. –Tom Chantler Oct 28 '13 at 9:40 1 Possible duplicate of How to avoid the "divide by zero" error in SQL? –Henrik Staun Poulsen Mar 10 at 21:06 add a comment| 5 Answers 5 active oldest votes up vote 25 down vote accepted A nicer way of doing this is to use NULLIF like this: Percentage = 100 * ClubTotal / NULLIF(AttTotal, 0) share|improve this answer answered Oct 28 '13 at 9:25 Tom Chantler 11.2k42939 2 This will return NULL if AttTotal = 0, while the query in question returns 0. –Nenad Zivkovic Oct 28 '13 at 9:31 3 @NenadZivkovic - Returning NULL for a divide by zero error probably makes more mathematical sense anyway though. –Martin Smith Oct 28 '13 at 9:34 I agree with @Martin Smith. If you have a situation where the ClubTotal is a number and AttTotal is zero, it doesn't make sense to return zero. Or any other number :-) Returning zero implies ClubTotal is zero and AttT
Using NULLIF() To Prevent Divide-By-Zero Errors In SQL By Ben Nadel on October 3, 2007 Tags: SQL Boyan Kostadinov just sent me a cool link to an article that is the final part in sql nullif a four part series that discusses the SQL NULL value. I haven't read the divide by zero error encountered in stored procedure first three parts yet, but there is a really cool tip in the fourth part on using NULLIF() to prevent divide-by-zero errors divide by zero error encountered. the statement has been terminated in a SQL call.The idea here is that, as with any other form of math that I know of, you cannot divide by zero in a SQL call. Therefore, running this code:
By Chris FehilyJul 25, 2008 Topics: ProductivitySuppose you http://www.peachpit.com/blogs/blog.aspx?uk=Avoiding-division-by-zero-with-NULLIF-Five-SQL-Tips-in-Five-Days-Part-5- want to calculate the male–female ratios for various school clubs, but you discover that the following query fails and issues a divide-by-zero error when http://blog.sqlauthority.com/2014/12/29/sql-server-puzzle-why-divide-by-zero-error/ it tries to calculate ratio for the Lord of the Rings Club, which has no women: SELECT club_id, males, females, males/females AS ratio divide by FROM school_clubs; You can use the function NULLIF to avoid division by zero. NULLIF compares two expressions and returns null if they are equal or the first expression otherwise. Rewrite the query as: SELECT club_id, males, females, males/NULLIF(females, 0) AS ratio FROM school_clubs; Any number divided by divide by zero NULL gives NULL, and no error is generated. Be sure to look back over my posts this week for more SQL tips. And for more on SQL, be sure to check out my book SQL: Visual QuickStart Guide, 3rd Edition. Share|Share on twitter Share on facebook Share on google Share on email✉ Other Things You Might LikeData at Work: Best practices for creating effective charts and information graphics in Microsoft Excel By Jorge CamõesBook $35.99Speaker Camp Workshop: Learn by Video By Russ Unger, Samantha StarmerDVD $39.99Windows 8: Visual QuickStart Guide By Barrie SosinskyBook $27.99 View Your Cart Account Sign InSearchSearch View Your Cart 👤 Account Sign In Create AccountTopicsAdobe PhotoshopAppleDesignDigital PhotographyDigital VideoGadgetsProductivity Web Design & Development StoreBookseBooksVideoWeb EditionsAuthorsSafariExploreAbout UsAcademic ResourcesAffiliatesArticlesBlogsNewslettersPressUser GroupsAboutAffiliatesContact UsJobsLegal NoticePrivacy PolicyPressPromotionsSite MapWrite for Us © 2016 Pearson Education, Peachpit. All rights reserved. 1301 Sansome Street, San Francisco, CA 94111
SERVER - Puzzle - Why Divide by Zero Error December 29, 2014Pinal DaveSQL, SQL Puzzle, SQL Server, SQL Tips and Tricks35 commentsEarlier I asked a puzzle SQL SERVER - Puzzle - Why Decimal is Rounded Up? and it was very well received by all of you. You can read various comments posted in the blog post and they are indeed very enlightening. After posting that blog post, I received quite a lots of emails asking for more puzzle similar to that. Based on your request, here is another puzzle which is very similar to the earlier puzzle but have a very different approach.The question is why following T-SQL gives a famous divide by zero error.SELECT 10000/(17/17/17) AS Result2;When we execute above query it gives following error:Msg 8134, Level 16, State 1, Line 1 Divide by zero error encountered.Here is the screenshot of the error.Hint: Read the comments in this blog post.I will be announcing the winner of this puzzle in my newsletter. There will be a surprise gift of USD 29. Leave your answers in the comment.Reference: Pinal Dave (http://blog.sqlauthority.com) 445 35 comments. Leave new 亂馬客 December 29, 2014 7:06 amInt / int = int 17/17 => 1 1/17 => 0 10000/0 => Divide by zero error encountered. Thanks.Reply Logan December 29, 2014 8:04 amThe denominator resolves to 0 since all numbers are integers and inside of the parenthesis the division is solved from left to right. 17/17 = 1 and then 1/17 = .0588 which is rounded to 0 to maintain the integer data type. Now the numerator is divided by 0 and throws an errorReply Anu December 29, 2014 8:59 amIn this situation SQL will translate 17 as an int so it is rounding the result to 0 and then attempting to divide 10000 by 0. Since division is a distribution and we can't distribute any number against 0. If you would like see SQL perform this math you can write "select 10000/(17.00/17.00/17.00) AS Result2" then we will get result in decimal or float however even this won't be completely accurate because SQL will do s