Calculated column incorrect syntax - sql

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)

Related

Divide by zero error encountered, but should be excluded by CASE

I have the following code and I am getting an error:
Divide by zero error encountered.
SELECT
CASE WHEN SUM([monthly_qty]) = 0 THEN 999
ELSE ROUND(SUM([monthly_buy] * ([monthly_markup]+100)/100),2) * SUM([monthly_qty] / [monthly_qty]) END as [monthly_total]
FROM [xxxxx].[dbo].[quote_items] WHERE docid='10152'
The field that is causing the error is the second [monthly_qty], just before the END of the CASE statement.
SUM([monthly_qty] / **[monthly_qty]**)
The value of monthly_qty is zero, so the error makes sense, but I am confused as this field is inside the CASE statement, so the expected result is 999
Any help greatly appreciated.
I can't understand reason of some parts of your code like this:
SUM([monthly_qty] / [monthly_qty])
The result is error when [monthly_qty] = Zero, And 1 for non zero.
Anyway, You can set a default value When "monthly_qty" is zero:
IIF([monthly_qty]= 0,'YOUR_DEFAULT_VALUE', [monthly_qty] / [monthly_qty])
Then:
SUM(IIF([monthly_qty]= 0,'YOUR_DEFAULT_VALUE', [monthly_qty] / [monthly_qty]))
Have a read at MS Docs, there is an example of your case under Remarks.
The CASE expression evaluates its conditions sequentially and stops
with the first condition whose condition is satisfied. In some
situations, an expression is evaluated before a CASE expression
receives the results of the expression as its input. Errors in
evaluating these expressions are possible. Aggregate expressions that
appear in WHEN arguments to a CASE expression are evaluated first,
then provided to the CASE expression. For example, the following query
produces a divide by zero error when producing the value of the MAX
aggregate. This occurs prior to evaluating the CASE expression.
The reason is simple. 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.
The simplest solution (and essentially the "standard" approach) is to use NULLIF():
SUM([monthly_qty] / NULLIF([monthly_qty], 0))
However, I wonder if you really intend:
SUM([monthly_qty]) / NULLIF(SUM([monthly_qty]), 0)

isnull(#variable, 1) but for 0 instead of null

I have an equation that multiplies loads of variables together, if one of those variables is 0 then I don't want it included in the equation by substituting it for 1 which won't affect the result.
A case when - then, statement for each variable validating if they're greater than 0is a bit clunky.
Is there a similar function like IsNull where if the variable is 0 then it returns an alternate value?
--edit #Backs answer is right but apparently after sql 2012 iif was taken out, when i try to write the statement there is a syntax error at the '=' sign. Is there a replacement for iif after sql-2012?
IIF(#variable = 0, 1, #variable)

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.

SQL How to check if field value is >0 (case when)?

I try to do an aggregation with two fields but only if the value of one field is not 0.
SELECT
CASE WHEN (CC90_StatisticQueueData.NBR_OF_CONVERSATIONS)>0
THEN (CC90_StatisticQueueData.SUM_WAITING_DURATION / CC90_StatisticQueueData.SUM_WAITING_DURATION)
FROM
CC90_StatisticQueueData
I always receive **ERROR**:
Incorrect syntax near the keyword 'FROM'.`
and don't know how to fix it.
Thanks in advance.
Use the END Keyword in CASE Statement :
SELECT CASE WHEN (CC90_StatisticQueueData.NBR_OF_CONVERSATIONS)>0
THEN (CC90_StatisticQueueData.SUM_WAITING_DURATION /
CC90_StatisticQueueData.SUM_WAITING_DURATION) END
FROM CC90_StatisticQueueData

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]