Divide by zero error encountered sql. How to continue on exception so that rows Thank you.


Divide by zero error encountered sql The query works fine when I don't include b. Those are the only two values, and the only two tables you have to worry about. CurBill)/t. For data-change operations (INSERT, UPDATE), its effect also depends on whether strict SQL mode is enabled. As for the error, I'm not sure how to solve it as the divisor shouldn't return zero. Although as Gordon Linoff writes in his answer, SQL Server may rearrange the calculations, so the CASE WHEN statements may actually be evaluated after the division is evaluated. The best way to avoid dividing by zero in SQL is to use the NULLIF function. I'll work through my SQL, which is moderately complex with multiple nested select statements. If you are looking for solution in M, then add conditional column: Set up Otherwise temporarily to dividend column, here I took [Value] column. Thank you! When using Amazon Redshift, divide by zero errors can be handled by using the ISNULL function. Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question. PoundsPerUnit=0 then checking ISNULL(l. Division by zero is a common problem when working with data in SQL. I haven't read the first three parts yet, but Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers; Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand; OverflowAI GenAI features for Teams; OverflowAPI Train & fine-tune LLMs; Labs The future of collective knowledge sharing; About the company Visit the blog Because without the order by, it can just take the first row it computes for the result set and (presumably) that row, by chance, contains non-zero InvL_Quantity and iu. In every single instance, the query is dividing by mm. Cookie Duration Description; cookielawinfo-checkbox-analytics: 11 months: This cookie is set by GDPR Cookie Consent plugin. To fix, don't divide by zero. if l. SELECT 'Accounts RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row. I'd like to perform division in a SELECT clause. 0 / NULLIF(c. You can use case expression, when the exponent is negative and the denominator is ZERO, use ABS() transform the negative exponent to a positive value. Data. Quantity, dbo. – ppn In SSMS, hit Ctrl+F to open the Find & Replace dialogue box. It avoids the division by zero in the denominator by adding the smallest possible Double value to it (Double. See examples of using NULLIF(), ISNULL(), CASE, SET ARITHABO Learn the causes and solutions of SQL divide by zero error, a common problem in data calculations. The division by zero is uniquely and reasonably determined as 1/0=0/0=z/0=0 in the natural extensions of fractions. All intermediate stages are saved as a view in SQL Server. Loan_Purpose_Desc = 'PURCHASE' then v. One aproach to deal with division by zero is to use NULLIF. What it means, and how to improve your code to not cuase Divide by Zero Errors in SQL Server. CurBill)*100) Price FROM [ClubEatc]. g. df as dialfileid,x. Telno, t. Alternatively, we return the average marks for a certain course: SELECT Learn how to avoid or handle division by zero errors in SQL queries using NULLIF or WHERE functions. The ERROR_FOR_DIVISION_BY_ZERO mode affects handling of division by zero, which includes MOD(N,0). com. It only fails some of the time, approx 50%, on an automatic refresh in Upon executing the SQL code provided, an error message is displayed stating Msg 8134, Level 16, State 1, Line 27 Divide by zero error encountered. If first expression returns the second, then NULL is returned, otherwise the value of the expression is. Cycle+'-'+y. Thanks for the tips. If you do decide to factor them in, you'll have to give them some nominal value. Here is the smallest query i could get, if i remove any of the components it starts working, it is enough to remove order by and it works. I believe this part is causing this problem: select DataDoc as Data Both of the solutions you proposed worked. We use it to store, retrieve, and manipulate data very efficiently. COALESCE(MAX(f. In the real world, everyone knows that if you divide by zero a wormhole will open up and swallow the universe. Use NULLIF to fix Divide by zero in SQL Server. (CASE(COALESCE(COUNT(column_name),1)) WHEN 0 THEN 1 ELSE Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question. filename,x. SubTotal) / NULLIF(sum(convert(int,SO_SalesOrder. That it's free to rearrange expressions In the real world, everyone knows that if you divide by zero a wormhole will open up and swallow the universe. Technical questions should be asked in the appropriate category. Hence it seems that length of **SearchStringtext** is evaluating to zero. StopQty / DATEDIFF( HOUR, tblBatchStatus. The count function does that, counting all the not null values, ignoring the null values. I haven't read the first three parts yet, but Here is my code: SELECT CASHIER_ID AS SERVER, CONVERT(VARCHAR(10),[DATETIME],111) AS DATE, SUM(GRAND_TOTAL) AS TOTAL_SALES, SUM(NUM_PEOPLE_PARTY) AS NUMBER_SERVED, SUM(GRAND_TOTAL) / A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions. Handling Divide by Zero Errors in SQL Server. We can use a CASE statement to perform conditional logic in SQL queries where we check if the number of markings is 0 and return NULL if it is. As for now I only come up with this method of avoiding the division by zero and null values. ISNULL sets a pre-defined value for a null value. However, I'm struggling to understand why. NULLIF( <expr1> , <expr2> ) returns NULL if expr1 is equal to expr2, otherwise returns expr1. Whether using CASE statements, A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions. 79) Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers; Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand; OverflowAI GenAI features for Teams; OverflowAPI Train & fine-tune LLMs; Labs The future of collective knowledge sharing; About the company I'm trying to calculate this. We can only guess at what query might be right. A CASE statement introduces conditional logic This is the error I'm receiving below: Divide by zero error encountered. The reason is simple. DateTimeCreated, dbo. That code can also be simplified a little (probably because it's already a simplified toy problem): What it does is if the value of the sum operator is equal 0 then the divisor is turn from zero into null and that will result in the entire equation to become null. 0 / COUNT(A. This article will We can use a CASE statement to perform conditional logic in SQL queries where we check if the number of markings is 0 and return NULL if it is. The cookie is used to store the user In this case the HAVING clause is not guaranteed to by applied before the division so you may or may not get a division by zero. Alternatively, we return the average marks for a certain course: SELECT Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question. But when you add the ORDER BY, more rows have to be produced and one or more of those contains a zero value for one or both of the above values. This function takes two arguments, and returns NULL if the two arguments are equal. Asking for help, clarification, or responding to other answers. Cycle Cycle, (((y. [dbo Minor points: Are you sure that's the formula you want to use? There is a built in AVG() function - the one you're currently using may not get the results you expect. To rename a column in a database table, we typically use the ALTER TABLE statement with the RENAME COLUMN clause. I've got the below piece of code in my query: CAST(COUNT(C. The Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question. I can't begin to understand what that calculation is trying to accomplish, so I don't know exactly what would be needed – The problem is that you cannot divide 1 by zero. e. Avoid division by zero in SQL Server. SqlException 'Divide by zero' which has to come from the order by clause. Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers; Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand; OverflowAI GenAI features for Teams; OverflowAPI Train & fine-tune LLMs; Labs The future of collective knowledge sharing; About the company Though this way the entire expression will evaluate to NULL in the case you should try to divide by zero. If the value of the first expression is NULL, then (in this example) that doesn't equal 0 and so NULL is returned. . Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers; Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand; OverflowAI GenAI features for Teams; OverflowAPI Train & fine-tune LLMs; Labs The future of collective knowledge sharing; About the company To avoid the overhead of maintaining code, the solution below feels tidiest to me. Warning: Null value is eliminated by an aggregate or other SET operation. Build your basic select syntax and then move to the stored procedure view to add in the case syntax. Epsilon, which is 4. It can lead to unexpected results and can even cause your query to fail. Pinal Dave is an SQL Server Performance Tuning Expert and independent consultant with over 22 years of hands-on experience. If this mode is not enabled, division by zero inserts NULL and You should check for the divisor ( denominator) and not for the divided (numerator) is not equal to 0 so for the first column you coudl try Guys, i discovered something that looks like sql bug, it gives division by zero where it should not be possible. In MySQL "Divide by zero error" is encountered when we try to divide one column with another column that contains some zero values. The only division operation in this procedure, has fn LENGTH(@SearchStringtext) as the divisor. [Total Sales], '$', ''), '(','-'), ')','')) as money)) alone in a select statement from your table. Loan_LoanAmt else 0 end ) as Numerator, sum ( case when v. Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers; Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand; OverflowAI GenAI features for Teams; OverflowAPI Train & fine-tune LLMs; Labs The future of collective knowledge sharing; About the company Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers; Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand; OverflowAI GenAI features for Teams; OverflowAPI Train & fine-tune LLMs; Labs The future of collective knowledge sharing; About the company A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions. Where that doesn't work is where you're checking both dates, but that is solved by a cast(CAST(countAta AS float) / CAST(DATEDIFF(day,@searchDate,@EndDate) AS float) as decimal(16,2) ) In my predicate, I am excluding any records that would cause a divide by zero error, and when I run my SQL query on that data set, everything runs fine. While performing Here, we describe some methods for avoiding division by zero in SQL queries. com/watch? The only issue is: it can occur that the values are zero or the same, and altough I do want to filter these out (see where clause), I still get a System. My understanding is that SUM will exclude NULL values but I don't know if this is faultless. NULLIF could turn a zero divisor into null thus rendering the complete calculation null, which is obviously what the OP wants. A CASE statement introduces conditional logic into your queries. Note that there won't be any division if a row isn't found (because there won't be any Group) The equivalent query on a SQL database should be Well, the real problem is that division by zero produce undefined results, so they should not be part of any calculation. I checked tables for integrity and no errors was found. First, you don't have to put all that logic in a sum function. Divide by zero errors can be a common pitfall in SQL Server, but with the right strategies, you can effectively prevent them and ensure your queries run smoothly. – Clockwork-Muse You don't show the grouping criteria but it's obvious at least one of the groups has one of the dates set to NULL over the entire group. – Thom A Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers; Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand; OverflowAI GenAI features for Teams; OverflowAPI Train & fine-tune LLMs; Labs The future of collective knowledge sharing; About the company Visit the blog SELECT * FROM RM_Sales_Union WHERE DOCDATE >= 'September 1, 2011' AND DOCDATE < 'October 1, 2011' AND CUSTNMBR = '2186020' That query results in an error: Msg 8134, Lev I'm new in sql server and write this query: SELECT y. First step towards the paradigm shift of writing Set Based code: _____ Stop thinking about what you want to do to a ROW While the NullIf code works, you may need to return something other than null depending on your business requirements, in that case you need a case statement Hi Johan, NULLIF is a function in SQL Server too, your purpose is the opposite of ISNULL. It's NULLIF not ISNULL, @Andrew . Divide by zero is one of the most common problems in database queries. How to install SQL Server for practice?https://www. Share Improve this answer Microsoft SQL Server (MSSQL) is a powerful relational database management system used by many organizations to store and manipulate data. capacity, 0)), 0) as [Value] If the values are already numbers and you are converting just to avoid division by 0, the try_convert() is not wrong, but it is misleading. One or more of them is either a view, or a table with a computed column When you select from them, SQL evaluates the expressions within the view (or computed columns) to generate the output data. It is essential to Microsoft SQL Server (MSSQL) is a powerful relational database management system used by many organizations to store and manipulate data. First one does the calculation when the value of COLUMN_B is non-zero and does a UNION to get 0 when it is zero. 0 * (speed_gps_kph - LAG(speed_gps_kph, 1) OVER (PARTITION BY machine_id ORDER BY event_timestamp)) / NULLIF(datediff(ss, LAG(event_timestamp, 1) OVER (PARTITION BY How to fix SQL Server divide by zero error encountered?It is not possible to divide a number by zero. callable) as callable, sum(x Learn about the cause and solution for the Hi Lynn, Yes there are rows where both t. "Diving by zero error" is a common problem while performing division in The result when there are no rows in the table so the denominator would be zero - gets converted to NULL so the whole expression ends up NULL. PurchaseOrderLine. PoundsPerUnit,1) is still going to return zero. There was a strange problem with division by zero at one of the stages (&quot;Di I'm using MS SQL 20012 and have a Stored Procedure the code is like this: USE [StatsAa] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo]. upload_date, sum(x. Alternatively, we return the average marks for a certain course: SELECT Thanks for the tips. I assume that this happens due to the /sum(iff(iscode=1,1,0)) where this presumably sometimes returns 0. We have to change our basic ideas for our space and world: select x. Here are some examples (yes this is a contrived sample :) ): DECLARE @Yak TABLE (ID INT, Val INT) When inexplicable errors like this pop up for me, I like to take as much out of the equation as possible. In my case I have to use divide operation at WHERE clause. See examples with investor data and P/E ratio calculation. But since they are equal they fall through the condition and give me '0%'. It allows you to handle zero division manually by checking the divisor's value. Assuming capacity is never negative and the values are numbers of some sort, you can simplify the logic to:. I'm new to SQL and would like to know the best way of dealing with dividing by zeros. Usually you should ask yourself why there is a /0-problem. This function can be used to check if the denominator is zero before performing the division. MySQL is an open-source relational database management system in short RDBMS. Try doing this portion here: SUM(Cast((replace(replace(replace (p. Total number of records excluding a divide by zero instance: 92,141,104 In SQL Server, preventing division by zero errors is vital for reliable data management. I have an update statement which has division operation and one row throws exception Divide by zero exception and I see that none of the rows got updated . By using our services, you agree to our use of cookies. [DriverVSComp] -- Add the Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question. If this isn't the desired behaviour, the null can be converted to a 1 by ISNULL(NULLIF ft, you'll be able to run the sql if you put it into a stored procedure and execute that from the query designer to test. The cookie is used to store the user consent for the cookies in the category "Analytics". Boyan Kostadinov just sent me a cool link to an article that is the final part in a four part series that discusses the SQL NULL value. These errors can disrupt calculations and lead to incorrect results or program crashes. I am working on some SQL query but it's throwing an error: Divide by zero error encountered. For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle. I'm sure there is no zero divider, because when I comment WHERE out, there is Learn five ways to deal with error Msg 8134 "Divide by zero error encountered" in SQL Server. One of my divides is based on a SUM of a column that does includes NULLS. In this example, NULLIF(impressions, 0) returns NULL whenever the impressions value is zero, avoiding a division by zero by substituting NULL where necessary. My code looks like: Select A, B, C, nullif(100*count / sum(count) OVER (partition by A, B), 0) as Share Any Divide-by-zero errors are a common issue encountered in programming, including in R Programming Language. Having looked Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers; Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand; OverflowAI GenAI features for Teams; OverflowAPI Train & fine-tune LLMs; Labs The future of collective knowledge sharing; About the company Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question. It might be possible that you are trying to search an Empty string. You could run Profiler to get a trace of what exactly is happening on the scenario where you're getting divide by zero. The error I'm getting is Divide by zero error encountered. Renaming Database Columns: A Step-by-Step Guide . After doing this change in editor [Value] to [Value]/[Units]. – cairnz Commented Nov 12, 2010 at 14:45 This blog provides solution to Issues pertaining to SQL, Business Intelligence and Data Analytics tools Such as SQL, Tableau, Alteryx, SSRS, SSIS, SSAS, TSQL, Power BI, Power View, Pivot Tables & Charts. The second one does that all in a single select The above is all just my opinion In conclusion, division-by-zero errors are common in SQL queries and can impact the accuracy and reliability of results. So, in your code where you have, for example sum(iff(iscode=1,1,0)), you can replace this with: If the variable you are capturing the return value into holds the wrong value, then either the proc is not returning the value you think it is, or your code is failing to capture the return value correctly. Process_FundingDt between @begDt and @endDt Thanks for the tips. Provide details and share your research! But avoid . best practices to avoid sql division by zero error using the nullif and isnull commands could be returning 0 because you are only checking for null and not zero. Have a look at the sql_mode error_for_division_by_zero. A simpler solution in SQL server if you are looking for a NULL output for divide by 0 is to use the NULLIF command around Solution for the error message "Msg 8134, Level 16, State 1, Line 7 Divide by zero error encountered" when performing division operation in SQL Server. This value's way too small to affect the value of the fraction for anything people would use SSRS for. CONVERT(varchar(9), CONVERT(DECIMAL(7,3),tblBatchActivity. But you should probably benchmark it by yourself. This returns null wherever Note the use of cast because otherwise we will have an integer division probably (if MovieRating is an integer). Fixing one of the most common errors Divide by zero errors are among the most common issues encountered in SQL queries, particularly when dealing with mathematical calculations or aggregations. Process_FundingDt between @begDt and @endDt and v. SELECT machine_id, speed_gps_kph, odometer_total, event_timestamp, 1. This may help: select [MyColumn] = case when Denominator = 0 then NULL else Numerator / Denominator end from ( select sum( case when v. So, in your code where you have, for example sum(iff(iscode=1,1,0)), you can replace this with: In this SQL tutorial, we learn some techniques to avoid the Divide By Zero error in SQL. For example, if you wanted to divide two then check before you divide by zero, and if you divisor would be zero, you need to think of another solution / another value to use instead Your problem is this: you're currently only checking for your one value being < 0 and then your return 0 - otherwise (including when that value is = 0 ) you return an expression which is a division by exactly that value. Here is the SQL: In this example, NULLIF(impressions, 0) returns NULL whenever the impressions value is zero, avoiding a division by zero by substituting NULL where necessary. Pinal has Below is the query I am trying to run using Microsoft SQL Server Management Studio: update [SG report MPD-4153] set [percent_paid] = (try_convert(float, [Savings This Season]) * 100 / try_convert( If you replace your division using NULLIF to set a NULL when there is divide by zero, then an ISNULL to replace the NULL with a 0 - or indeed whatever value you want it to. When I join some tables and use aggregate function I often have either null or zero values as the dividers. LastPassedBackToSalesDate) AS numeric(18, 2)) AS PBTSbutActived Both fields are of DateTime datatype. 0000,-0. In this example, every @Govind KamalaPrakash Malviya: Not necessarily, and if not, I'd expect the difference to be inessential. Total number of records generated (all combinations): 92,345,408. CurBill - t. However, there are several solution approaches to avoiding these errors such as using the NULLIF() function, CASE statement, or WHERE clause. Don't use the implicit-join syntax (comma-separated FROM clause) - always explicitly declare your joins. See examples of using NULLIF, ISNULL, CASE and SET ARITHABORT Divide-by-zero errors are a common issue encountered in programming, including in R Programming Language. These are not only effective but also simple to implement. He holds a Masters of Science degree Here's a simple way to control for divide-by-zero errors, without repeating yourself, using NULLIF(,0):. These approaches will surely avoid Division by Zero errors – but they do not solve the problem. CaseQTY or bm. NULLIF sets a null for pre-defined value. sum(SO_SalesOrder_Line. I know about the NULLIF function however I'm having a bit of trouble implementing it. The question is more or less were to place NULLIF such that it deals with all cases where a divisor can be null. gross) as leadsreceived, sum(x. youtube. PurchaseOrderHeader. My point is that the SQL never fails in Azure Data Studio or Power BI Desktop. SoldYR1 and t. While performing If StudentMarkId is one, you'll get zero when you subtract one. Date , tblBatchActivity. I've looked at a few other threads on this, but I'm still not sure. I think that something like this should solve your problem. Value * 1. If Total Working Hours is 0, what do you want to return for Face to Face Ratio? Strengthen you SQL Server Development Skills – Enroll to our Online Course! Check our online course titled “ Essential SQL Server Development Tips for SQL Developers ” (special limited-time discount @GustavoSchneider: if there are truly no views involved, and SELECT * FROM tabla_parte where ( [CONSUMO_DE_GAS_DAM3] *9300 * 1000 + [CONSUMO_DE_GASOIL_M3] * 9211 + [CONSUMO_DE_FUELOIL_T] * 10500 *1000 ) = 0 gives no rows, I'd still call this an optimizer bug. In SQL Server, it’s not good, but it’s not nearly as dramatic. NULLIF is an SQL function Pinal Dave is an SQL Server Performance Tuning Expert and independent consultant with over 22 years of hands-on experience. He holds a Masters of Science degree and numerous database certifications. QuantityReceived, dbo . CaseQTY. Custom1)), 0) Cookies help us deliver our services. Solution / Work Around: There are three ways to avoid the "Division by zero encountered" error in your SELECT statement and these are as follows: Sample 2 has: NULLIF(Count(date_record_entered), 1) Instead, try: NULLIF(Count(date_record_entered), 0) NULLIF will return the value in the first argument unless it is equal to the value in the second argument. I encounter This is not caused by the union, but rather by the nature of the object TLP, or TLP_S1, or TLP_S2. LastEdited Divide by Zero Errors and how to avoid them covering SQL, BigQuery, Excel, Power BI and DAX. Search the query for all instances of /. Share Improve this answer Chad Callihan has a few methods for us to avoid dividing by zero:. This code: SUM([monthly_qty]) = 0 Does not prevent a divide-by-zero in this code: SUM([monthly_qty] / [monthly_qty]) The expressions are different. It leads to infinity. I love Postico, but it is not made by the creators of Postgres, per se. A nice way to avoid division by zero in this case is to use NULLIF:. One more thing is that if you want to calculate 2^(-2), you can write SELECT POWER(2*1. ItemUnit_MainUnitQuantity values. DateExpected, dbo. Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers; Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand; OverflowAI GenAI features for Teams; OverflowAPI Train & fine-tune LLMs; Labs The future of collective knowledge sharing; About the company I am having problems with dividing by zero. loaded) as leadsuploaded, sum(x. This will tell you everywhere that division is happening, plus some false positives. A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions. Techniques like NULLIF(), CASE statements, and zero-denominator filtering ensure If you are getting divide by zero then your query must be wrong. The basic steps that SQL Server uses to process a single SELECT statement include the following. [Billed Item] <> 0. The parser scans the SELECT statement and breaks it into logical units such as keywords, expressions, operators, and identifiers. Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers; Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand; OverflowAI GenAI features for Teams; OverflowAPI Train & fine-tune LLMs; Labs The future of collective knowledge sharing; About the company Handling Divide by Zero Errors in SQL Server. Employing CASE Statements. How to continue on exception so that rows Thank you. SoldYR2 are zero. date as date,x. Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers; Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand; OverflowAI GenAI features for Teams; OverflowAPI Train & fine-tune LLMs; Labs The future of collective knowledge sharing; About the company SELECT dbo. DateChanged) * 100. try_convert() suggests that the conversion might In SQL Server there isn't an easy way to do this as Ordering or other functions may affect the row order after the Put in some exception handling into your procedure using case statements and output an exception value on the broken rows. It can cause unexpected errors or bad results. This article will explore the causes of divide-by-zero errors in R and provide methods to handle and preve I am building a data mart using the data pipeline in DBT. For example: SELECT CASE WHEN ISNULL(denominator, 0) = 0 THEN 0 ELSE numerator/denominator END AS result FROM table_name; This code will return 0 if the The OP wrote "I thought I eliminated all PCT values that were zero with the CASE WHEN statement", because the CASE statement appears to prevent the divide by zero calculation. When I try using nullif, I end up with a zero or one for the calculated value. If the denominator is zero I would like the value to be zero. 94e-324). Using NULLIF Function. lampslwp ntt dvp kpszq jozwbp othlxu ekmklo dnlvjkv jyqr uynrku