Divide By Zero Error Mssql
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 sql server divide by zero error encountered posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss divide by zero error encountered in sql server 2012 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 divide by zero error encountered. in sql server 2008 a 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 divide by zero error encountered excel a CASE 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
Msg 8134 Level 16 State 1 Line 1 Divide By Zero Error Encountered
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 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.2k42940 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 :-) Return
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
Oracle Sql Divide By Zero
is the final part in a four part series that discusses the divide by zero error encountered in stored procedure SQL NULL value. I haven't read the first three parts yet, but there is a really cool tip in divide by zero error encountered. the statement has been terminated the fourth part on using NULLIF() to prevent divide-by-zero errors in a SQL call.The idea here is that, as with any other form of math that I know of, you cannot http://stackoverflow.com/questions/19631017/simple-way-to-prevent-a-divide-by-zero-error-in-sql divide by zero in a SQL call. Therefore, running this code:
8 knows that division by zero is a no-no, or at least they should. Everybody who's used SQL for any amount of time knows that we can use a CASE expression to prevent division by zero. http://www.itjungle.com/fhg/fhg051210-story02.html But do you know the other way to prevent division by zero in SQL? Assume a database file called SomeTable, with fields called Quantity and Amount. Let's divide Amount by Quantity. First, here's the usual case statement. https://msdn.microsoft.com/en-us/library/ms184341.aspx select itnbr, Amount, Quantity, case when Quantity <> 0 then Amount / Quantity else 0 end from SomeTable If Quantity is not zero, the division takes place. If Quantity is zero, the calculation yields a zero. We divide by could just as easily return any other numeric value, the value one, for example. select itnbr, Amount, Quantity, case when Quantity <> 0 then Amount / Quantity else 1 end from SomeTable The other way to prevent division by zero is to use the NULLIF function. NULLIF requires two arguments. If the arguments are equal, NULLIF returns a null value. If they are not equal, NULLIF returns the first value. Here's the SQL. select itnbr, divide by zero Amount, Quantity, Amount / nullif(Quantity,0) from SomeTable If Quantity is not zero, the two arguments do not match, and the resulting calculation is Amount divided by Quantity. But if Quantity is zero, the arguments do match, and the calculation is Amount divided by null, which yields a null value. If you use this technique, be sure to put the zero in the second argument of NULLIF. Post this story to del.icio.us Post this story to Digg Post this story to Slashdot Sponsored By SEQUEL SOFTWARE SEQUEL ViewPoint--Data Access & Analysisfor Power Systems Servers Easy to use by IT and end users Real-time data access and analysis Queries, reports, pivot tables, drill-down analysis, kpi dashboards IBM i-centric with multiplatform data access Expert support and training Secure data access GUI, Web & green screen interface SEQUEL--Fast, efficient & cost-effective data analysis Click here for a FREE Information Kit! Senior Technical Editor: Ted Holt Technical Editor: Joe Hertvik Contributing Technical Editors: Erwin Earley, Brian Kelly, Michael Sansoterra Publisher and Advertising Director: Jenny Thomas Advertising Sales Representative: Kim Reed Contact the Editors: To contact anyone on the IT Jungle Team Go to our contacts page and send us a message. Sponsored Links Help/Systems: Robot/SCHEDULE Enterprise for UNIX, Linux, Windows & i looksoftware: RPG Open Access Webinar -
resources Windows Server 2012 resources Programs MSDN subscriptions Overview Benefits Administrators Students Microsoft Imagine Microsoft Student Partners ISV Startups TechRewards Events Community Magazine Forums Blogs Channel 9 Documentation APIs and reference Dev centers Retired content Samples We’re sorry. The content you requested has been removed. You’ll be auto redirected in 1 second. Microsoft SQL Server Language Reference Transact-SQL Reference (Database Engine) SET Statements (Transact-SQL) SET Statements (Transact-SQL) SET ARITHIGNORE (Transact-SQL) SET ARITHIGNORE (Transact-SQL) SET ARITHIGNORE (Transact-SQL) SET ANSI_DEFAULTS (Transact-SQL) SET ANSI_NULL_DFLT_OFF (Transact-SQL) SET ANSI_NULL_DFLT_ON (Transact-SQL) SET ANSI_NULLS (Transact-SQL) SET ANSI_PADDING (Transact-SQL) SET ANSI_WARNINGS (Transact-SQL) SET ARITHABORT (Transact-SQL) SET ARITHIGNORE (Transact-SQL) SET CONCAT_NULL_YIELDS_NULL (Transact-SQL) SET CONTEXT_INFO (Transact-SQL) SET CURSOR_CLOSE_ON_COMMIT (Transact-SQL) SET DATEFIRST (Transact-SQL) SET DATEFORMAT (Transact-SQL) SET DEADLOCK_PRIORITY (Transact-SQL) SET FIPS_FLAGGER (Transact-SQL) SET FMTONLY (Transact-SQL) SET FORCEPLAN (Transact-SQL) SET IDENTITY_INSERT (Transact-SQL) SET IMPLICIT_TRANSACTIONS (Transact-SQL) SET LANGUAGE (Transact-SQL) SET LOCK_TIMEOUT (Transact-SQL) SET NOCOUNT (Transact-SQL) SET NOEXEC (Transact-SQL) SET NUMERIC_ROUNDABORT (Transact-SQL) SET OFFSETS (Transact-SQL) SET PARSEONLY (Transact-SQL) SET QUERY_GOVERNOR_COST_LIMIT (Transact-SQL) SET QUOTED_IDENTIFIER (Transact-SQL) SET REMOTE_PROC_TRANSACTIONS (Transact-SQL) SET ROWCOUNT (Transact-SQL) SET SHOWPLAN_ALL (Transact-SQL) SET SHOWPLAN_TEXT (Transact-SQL) SET SHOWPLAN_XML (Transact-SQL) SET STATISTICS IO (Transact-SQL) SET STATISTICS PROFILE (Transact-SQL) SET STATISTICS TIME (Transact-SQL) SET STATISTICS XML (Transact-SQL) SET TEXTSIZE (Transact-SQL) SET TRANSACTION ISOLATION LEVEL (Transact-SQL) SET XACT_ABORT (