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 divide by zero error encountered. sql server 2012 of this site About Us Learn more about Stack Overflow the company Business t-sql prevent divide by zero error Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask t-sql divide by zero check Question x Dismiss Join the 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 minute: Sign
T-sql Handle Divide By Zero
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 statement to stop this from happening. Is there a simpler way of doing this? Here's my code: Percentage = CASE WHEN AttTotal <> divide by zero error encountered sql server 2008 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. 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 26 down vote accepted A nicer way of
3001-4000 Messages 4001-5000 Messages 5001-6000 Messages 6001-7000 Messages 7001-7500 Messages 7501-8000 Messages 8001-8500 Messages 8501-9000 Messages 9001-9500 Messages 9501-10000 Messages 10001-10500 Messages 10501-11000 Messages 11001-11500 Messages 11501-12000 Messages 12001-13000 Messages 13001-13500 divide by zero error encountered excel Messages 14001-14500 Home>SQL Server Error Messages> Msg 8134 - Divide by zero error
Msg 8134 Level 16 State 1 Line 1 Divide By Zero Error Encountered
encountered SQL Server Error Messages - Msg 8134 - Divide by zero error encountered SQL Server Error Messages - Msg
Oracle Sql Divide By Zero
8134 Error Message Server: Msg 8134, Level 16, State 1, Line 1 Divide by zero error encountered. Causes: This error is caused by performing a division operation wherein the denominator or the divisor http://stackoverflow.com/questions/19631017/simple-way-to-prevent-a-divide-by-zero-error-in-sql is 0. This error is not encountered when the denominator or divisor is NULL because this will result to a NULL value. Solution / Work Around: There are three ways to avoid the "Division by zero encountered" error in your SELECT statement and these are as follows: CASE statement NULLIF/ISNULL functions SET ARITHABORT OFF and SET ANSI_WARNINGS OFF Using the CASE statement, your query will look like http://www.sql-server-helper.com/error-messages/msg-8134.aspx the following: SELECT CASE WHEN [Denominator] = 0 THEN 0 ELSE [Numerator] / [Denominator] END AS [Percentage] FROM [Table1] If the denominator or divisor is 0, the result becomes 0. Otherwise, the division operation is performed. Using the NULLIF and ISNULL functions, your query will look like the following: SELECT ISNULL([Numerator] / NULLIF([Denominator], 0), 0) AS [Percentage] FROM [Table1] What this does is change the denominator into NULL if it is zero. Then in the division, any number divided by NULL results into NULL. So if the denominator is 0, then the result of the division will be NULL. Then to return a value of 0 instead of a NULL value, the ISNULL function is used. Lastly, using the SET ARITHABORT and SET ANSI_WARNINGS, your query will look like the following: SET ARITHABORT OFF SET ANSI_WARNINGS OFF SELECT [Numerator] / [Denominator] With both ARITHABORT and ANSI_WARNINGS set to OFF, SQL Server will return a NULL value in a calculation involving a divide-by-zero error. To return a 0 value instead of a NULL value, you can put the division operation inside an ISNULL function: SET ARITHABORT OFF SET ANSI_WARNINGS OFF SELECT ISNULL([Numerator] / [Denominator], 0) Related Articles : Freque
SERVER - How to Fix Error 8134 Divide by Zero Error Encountered August 27, 2016Pinal DaveSQL Tips and Tricks5 commentsHere is one of the most popular questions: How to overcome (Error 8134) Divide by Zero Error Encountered in SQL Server?Before we see the answer of this question, let us http://blog.sqlauthority.com/2016/08/27/sql-server-fix-error-8134-divide-zero-error-encountered/ see how to recreate this error.Run following script in SQL Server Management Studio window.DECLARE @Var1 FLOAT; DECLARE @Var2 FLOAT; SET @Var1 = 1; SET @Var2 = 0; SELECT @Var1/@Var2 MyValue;When you execute above script you will see that it will throw error 8134. Msg 8134, Level 16, State 1, Line 6 Divide by zero error encountered. Here is the screenshot of the error.Now there are multiple ways to avoid this error to happen. divide by We will see two of the most popular methods to overcome this error.Method 1: Use NullIf FunctionHere is the SQL script with NullIf FunctionDECLARE @Var1 FLOAT; DECLARE @Var2 FLOAT; SET @Var1 = 1; SET @Var2 = 0; SELECT @Var1/NULLIF(@Var2,0) MyValue;When you use the NULLIF function, it converts the zero value to Null and leading to the entire result set to be NULL instead of an error.Method 2: Use Case StatementHere is the SQL script divide by zero with CASE StatementDECLARE @Var1 FLOAT; DECLARE @Var2 FLOAT; SET @Var1 = 1; SET @Var2 = 0; SELECT CASE WHEN @Var2 = 0 THEN NULL ELSE @Var1/@Var2 END MyValue;When you use CASE statement, it converts the zero value to Null and leading to the entire result set to be NULL instead of an error.Let me know if you have any other alternate solution. I will be happy to publish in the blog with due credit.Reference: Pinal Dave (http://blog.sqlauthority.com) Tags: SQL Error Messages, SQL Server330Related Articles SQL SERVER - Performance Comparison - INSERT TOP (N) INTO Table - Using Top with INSERT March 3, 2010Pinal Dave SQL SERVER - Unable to Bring SQL Cluster Resource Online - Online Pending and then Failed August 14, 2015Pinal Dave SQL SERVER - 2005 - SSMS - View/Send Query Results to Text/Grid/Files July 19, 2007Pinal Dave 5 comments. Leave new subbu444 August 27, 2016 10:27 amHi,Please check the below code to avoid 8134 error.DECLARE @Var1 FLOAT; DECLARE @Var2 FLOAT; SET @Var1 = 1; SET @Var2 = "; -0, 1, NULL," IF(@Var2=0) SELECT NULL; ELSE SELECT @Var1/@Var2;Regards, SubbaReddy AVReply Pinal Dave August 27, 2016 4:19 pmThat should work as well. Thanks for sharing.Reply Azhar August 28, 2016 12:30 ambegin try select @var1/@var2 end try begin catch if error_number() = 8134 select null else select error_number() end catchRepl