Divide by zero error encountered in SQL Server 2014 - sql

In this specific part of a query I'm trying to create, I'm getting a "divide by zero" error. I believe this is because I'm trying to divide ARTran.Qty by a null. I'm unsure how I can work around this... (I'm using Cast as in the next part of the query I'm trying to using this number as a decimal
(CASE WHEN ([ARTran].[UOM] LIKE 'EACH')
THEN CAST([ARTran].[Qty] AS VARCHAR(30))
ELSE CAST((ARTran.Qty / InventoryItem.UsrLength) AS VARCHAR(30))
END)
Can someone point me in the right direction? I've looked at a few other threads on this, but I'm still not sure how to solve this.

Use NULLIF to get around this.
(CASE WHEN ([ARTran].[UOM] LIKE 'EACH')
THEN CAST([ARTran].[Qty] AS VARCHAR(30))
ELSE CAST((ARTran.Qty / NULLIF(InventoryItem.UsrLength, 0) AS VARCHAR(30))
END)

Related

SQL CASE statement needs to handle Text

Apologies if this has been asked before - I've spent a couple of hours searching but not found anything that's helped.
It's quite simple really - I've been asked to create a query which includes a field that when it was set up (not by me) was created as a VARCHAR instead of an INT.
I need to do some calculations on this field, however some users have been entering text into it, so the calculations fail as it can't convert the data to an INT.
Is there anything I can add to a CASE statement to handle where there's text?
I was thinking something like the below, but don't know what the actual code is:
CASE
WHEN [Field1] IS TEXT THEN 1 ;
ELSE [Field2] as [Chose name]
END
Edit: Note that this is in MS SQL Server.
Thanks.
In SQL Server, you can use try_convert() and isnull() for this:
isnull(try_convert(int, field), 1)
try_convert() attempts you cast field to an int. When that fails, null is returned; you can trap that with isnull() and turn the result to 1 instead.
Note that this only works as long as field is not null (otherwise, you would get 1 as a result).
In SQL Server
Declare #Salary varchar(100);
Set #Salary = 50000;
Select Case when ISNUMERIC(#Salary) = 1 then 1
else 0 end as [Check]
May be this will be Helpful.

Calculated column incorrect syntax

I want to create a calculated column in SQL Server 2017 Developer as
ISNULL([Nominator]/NULLIF([Denominator]), 0) AS [Recidivation]
but this raises an error
Incorrect syntax near '('.
This syntax works:
[Nominator]/[Denominator] AS [Recidivation]
I must be blind or something but have somebody any idea how I can't prevent division by zero in a calculated column as shown above?
Help would be appreciated. Thanks.
You need an additional , 0. I also prefer the standard COALESCE() function. So:
COALESCE(Nominator / NULLIF(Denominator, 0), 0) AS [Recidivation]
I also think the intention might be clearer with case:
(CASE WHEN Denominator = 0 or Denominator IS NULL THEN 0
ELSE Nominator / Denominator
END)

Division in SQL Server

I am trying to use division in SQL Server. I have tried the code snippet below.
SELECT m.[Profit] / f.[Target]
And I have also tried
SELECT SUM(m.[Profit] / f.[Target])
but I get the error
Divide by zero error encountered.
Warning: Null value is eliminated by an aggregate or other SET operation.
What is the reason for this and how can I fix it? Suggested fixes online say that this code should work but it doesn't.
Thanks
Use NULLIF to avoid divided by zero exception
SELECT SUM(m.[Profit] / NULLIF(f.[Target],0))
When denominator is zero then it will be replaced with NULL
If I were trying to determine the % of a goal (inferred by profit /target columns), I would try something like:
SELECT
CASE WHEN ISNULL(f.[Target],0) = 0 THEN NULL -- or whatever you need to return when the target value is undefined, or it is 0
ELSE m.[Profit] / f.[Target] END AS [result]
The error you get specifies exactly what is the problem. Your code is trying to divide by zero, which is mathematically impossible.
Please NULLIF to avoid this error.

Excel to SQL Case

I am trying to convert a really convoluted EXCEL sheet into SQL stored procedure. There is one last formula that is kicking my butt though:
=IF(AND (W4+H4<=0,IF(E4-H4-S4>0,E4-H4-S4,0)<=0) ,0, IF(W4+H4<IF(E4-H4-S4>0,E4-H4-S4,0) ,IF(W4+H4<0,0,W4+H4),IF (E4-H4-S4>0, IF(W4>H4,E4-S4,H4+AF4) ,H4)))
I have created the equations in SQL to sort out the following:
W4.value1
H4.value2
E4_H4_S4.value3
AF4.value4
(as those were already determined earlier in the stored procedure. So I just put them back in as Outer applies for this specific subquery)
However, I have no real idea how to read this Excel formula, and the person that wrote it was fired, so I can't go to her for help.
To me, it reads like this:
=IF(AND (W4+H4<=0,IF(E4-H4-S4>0,E4-H4-S4,0)<=0),0, IF(W4+H4< (IF(E4-H4-S4>0 then E4-H4-S4 else 0) then (IF(W4+H4<0 then 0 else W4+H4) then (IF(E4-H4-S4>0, then (IF(W4>H4 then E4-S4 else H4+AF4) else H4)) )))))
However, I doubt this is correct, as I suck at EXCEL
Anyone able to help me decipher this?
I cannot test this currently, but I think you want something like this:
CASE
WHEN W4+H4<=0 AND (CASE WHEN E4-H4-S4>0 THEN E4-H4-S4 ELSE 0 END)<=0
THEN 0
ELSE
WHEN W4+H4< (CASE WHEN E4-H4-S4>0 THEN E4-H4-S4 ELSE 0 END)
THEN (CASE WHEN W4+H4<0 THEN 0 ELSE W4+H4 END)
ELSE (CASE WHEN E4-H4-S4>0 THEN (CASE WHEN W4>H4 THEN E4-S4 ELSE H4+AF4 END) ELSE H4 END)
END
I really had no idea how to format this, but I believe everything should work as intended according to the ridiculous IF() function, of course substituting your converted values and so on.
EDIT:
This is a straight conversion from EXCEL to SQL so please for the love of programming fix this so 1. someone else can read it (i.e. what if you got fired and you left the next poor soul the same thing you got stuck with) and 2. get rid of the unnecessary checks.

T-SQL 2005 - Divide by zero error encountered

I am trying to get some percentage data from a stored procedure using code similar to the line below. Obviously this is going to cause a (Divide by zero error encountered) problem when base.[XXX_DataSetB] returns 0 which may happen some of the time.
Does anyone know how i can deal with this in the most efficient manner?
Note: There would be about 20+ lines looking like the one below...
cast((base.[XXX_DataSetB] - base.[XXX_DataSetA]) as decimal) /
base.[XXX_DataSetB] as [XXX_Percentage]
I guess it depends on what behaviour you expect when XXX_DataSetB is zero.
If you don't want any rows returned that might cause an error, you can easily filter out those rows by adding where XXX_DataSetB <> 0 to your query.
If you would like problem rows to be NULL then you could use a case statement, or something like this:
cast(([XXX_DataSetB] - [XXX_DataSetA]) as decimal) / nullif([XXX_DataSetB], 0)
I'd force the average to null by making the bottom term null instead of zero:
cast((base.[XXX_DataSetB] - base.[XXX_DataSetA]) as decimal)
/ case when base.[XXX_DataSetB] = 0 then null
else base.[XXX_DataSetB] end as [XXX_Percentage]