how to use is null with case statements - sql

Hi can any one say me how to write query in sql server management studio for statement =>if(isnull("GROSS_SF")=1 or "GROSS_SF"=0,0,"GROSS_SALES"/"GROSS_SF")
by using case statement :
CASE
WHEN ("GROSS_SF"=1 or "GROSS_SF"=0) isnull then 0
else "GROSS_SALES"/"GROSS_SF"
end/* i am getting error if i write it like this */
thanks in advance

If you want to avoid a divide by zero, while at the same time also handling NULL, then you may try the following logic:
CASE WHEN COALESCE(GROSS_SF, 0) = 0
THEN 0
ELSE GROSS_SALES / GROSS_SF END AS output
To be clear, the above logic would return a zero value should either the GROSS_SF be zero or NULL.

I think what are you looking for is IFNULL, look here https://www.w3schools.com/sql/sql_isnull.asp

You are close. You can do:
(CASE WHEN "GROSS_SF" IS NULL OR "GROSS_SF" = 0 THEN 0
ELSE "GROSS_SALES" / "GROSS_SF"
END)
In SQL, divide by zero errors are often avoided using NULLIF():
"GROSS_SALES" / NULLIF("GROSS_SF", 0)
However, that returns NULL rather than 0. If you really want zero:
COALESCE("GROSS_SALES" / NULLIF("GROSS_SF", 0), 0)
This is a bit shorter to write and almost equivalent to your version (this returns 0 if "GROSS_SALES" is NULL, which your version does not).

You can use NULLIF() to prevent arithmetic divide by zero errors :
select . . . ,
GROSS_SALES / nullif(GROSS_SF, 0)
from table t;
However, this would return null instead of error, if you want to display 0 instead then you need to use isnull() (MS SQL Specific) or coalesce() (SQL Standard).
So, you can express it :
select . . . ,
isnull(GROSS_SALES / nullif(GROSS_SF, 0), 0)
from table t;

Related

Divide Zero error when calculating Sales % in SQL

I keep getting the following error message:
Msg 8134, Level 16, State 1, Line 1 Divide by zero error encountered.
I've seen some posts about using IFNULL which I have tried.
For example:
,Case when a.DiscountReasonCode = 'RL' then IFNULL(((a.ORIGINALRETAIL-a.RetOne) / (a.ORIGINALRETAIL) * 100),0) end as [PctSB]
But this returns the following error:
'IFNULL' is not a recognized built-in function name.
What I'm doing is trying to calculate the proper sales percent and then find and compare it to what is already in the table to find errors or missing Sales %'s.
I'm not sure where I'm going wrong but any help would be greatly appreciated.
SELECT
a.packnum
,a.description
,a.CatID
,a.PctSavings
,Case when a.DiscountReasonCode = 'RL' then (a.ORIGINALRETAIL-a.RetOne) / (a.ORIGINALRETAIL) * 100
end as [PctSB]
FROM PIC704Current a Join CatalogInfo b ON (a.CatID = b.Catalog) and (a.Year = b.MailYear)
WHERE
b.MediaId in('CAT Catalog','SCAT Sale Catalog','SSTF Sale Statement Stuff','STUF Statement
Stuffer','PKG Package Insert','SPKG Sale Pkg Insert')
and a.DiscountReasonCode = 'RL'
and a.year >='2020'
and (Case when a.PctSavings <> (a.ORIGINALRETAIL-a.RetOne)/a.ORIGINALRETAIL*100 then 'False' else
'True' END) = 'False'
Thanks
Wrapping a divide in a null test will not get rid of the divide by 0 error. Instead, you need to check whether the value being divided by is 0 and return a different value instead. You can do this using IIF:
IIF(a.ORIGINALRETAIL = 0, 0, 100.0 * (a.ORIGINALRETAIL-a.RetOne) / a.ORIGINALRETAIL)
You can use coalesce or isnull . MySQL uses ifnull
case
when a.DiscountReasonCode = 'RL' then (a.ORIGINALRETAIL-a.RetOne) /
NULLIF(a.ORIGINALRETAIL, 0) * 100
end as [PctSB]
Msg 8134, Level 16, State 1, Line 1 Divide by zero error encountered.
This error suggests that your denominator is 0 so you can change it to NULL using a case expression.
'IFNULL' is not a recognized built-in function name.
In SQL Server , you can use ISNULL or Coalesce
CASE WHEN a.DiscountReasonCode = 'RL'
THEN ISNULL(((a.ORIGINALRETAIL-a.RetOne)/(CASE WHEN a.ORIGINALRETAIL = 0
THEN NULL
ELSE a.ORIGINALRETAIL
END) * 100),0)
END AS [PctSB]
I think the posts you've seen mentioning IFNULL might actually have been mentioning NULLIF, you just remembered it the wrong way round. One of the typical tricks to avoid a DIV/0 error in sqlserver:
some_number / NULLIF(other_number_maybe_zero, 0)
If the divisor is 0, it is converted to null, meaning the result is null rather than an error
In standard SQL you can do it too:
some_number / CASE WHEN other_number_maybe_zero = 0 THEN NULL ELSE other_number_maybe_zero END
It's just a bit more wordy
The logic that you want is provided by NULLIF():
(a.ORIGINALRETAIL - a.RetOne) * 100.0 / NULLIF(a.ORIGINALRETAIL, 0)) * 100) as [PctSB]
Using the standard SQL function NULLIF() is the simplest way to do what you want, and I recommend that you write the logic this way. It replaces any 0 value with NULL -- thereby avoiding the divide-by-zero.

Case expression degrading performance in Correlated Sub query

SUM((CASE WHEN ([t2].[__measure__8] > 1) THEN (CASE WHEN [t2].[__measure__8] = 0 THEN NULL ELSE (CAST([t2].[__measure__9] as float) / [t2].[__measure__8]) END) ELSE [t2].[__measure__9] END))
when I use this calculation above Correlated Subquery which has a group by too . Its is taking more than 5 minutes to receive the result set. Moreover, the inner subset of query retrieves in just 4 sec. Am I missing something else?
What is the best way to write this case expression?
Sub case statement looks like unnecessary. You can remove CASE WHEN [t 2].[_ measure _8] = 0
SUM(CASE
WHEN ([t2].[__measure__8] > 1) THEN (CAST([t2].[__measure__9] as float) / [t2].[__measure__8])
ELSE [t2].[__measure__9]
END )
Since you only have 1 condition on your case statement, I suggest to use iif function instead.
sum(
iif([t2].[__measure__8] > 1,
iif([t2].[__measure__8] = 0, null, cast([t2].[__measure__9] as float)/[t2].[__measure__8]),
[t2].[__measure__9])
)
Try to break down your query by using subqueries, remove the sum() first then check the improvements.

divisor is equal to zero in sql need to add some sort of try catch

I have this query that i get errors when data gets loaded to this table and the divisor is zero which is the b. living_units column. I want to know if I can do some sort of try catch or if its zero to show null instead of failing or something that will help not error out?
SELECT a.SERVICE_TYPE_GRP,
a.HSIA_TYPE,
a.STAT_DYN_IND,
a.VIDEO_IND,
a.VOICE_IND,
a.CUST_CNT,
b.LIVING_UNIT_CNT,
a.DT_MODIFIED,
a.CUST_CNT / b.LIVING_UNIT_CNT AS ALLRGN_TK_RT_PCT
FROM ( SELECT lp.SERVICE_TYPE_GRP SERVICE_TYPE_GRP,
lp.HSIA_TYPE HSIA_TYPE,
lp.STAT_DYN_IND STAT_DYN_IND,
lp.VIDEO_IND VIDEO_IND,
lp.VOICE_IND VOICE_IND,
lp.DT_MODIFIED DT_MODIFIED,
SUM (lp.CUST_CNT) CUST_CNT
FROM RPT_SUBSCR_REGION_DTL lp
GROUP BY SERVICE_TYPE_GRP,
HSIA_TYPE,
STAT_DYN_IND,
VIDEO_IND,
VOICE_IND,
DT_MODIFIED) a,
( SELECT DT_MODIFIED, SUM (LIVING_UNIT_CNT) LIVING_UNIT_CNT
FROM RPT_REGION_CUST_DTL
WHERE dt_modified = (SELECT dt_modified
FROM ls_dt_modified
WHERE NAME = 'RPT_REGION_CUST_DTL')
GROUP BY DT_MODIFIED) b
WHERE a.DT_MODIFIED = b.DT_MODIFIED;
The error i get is ORA-01476: divisor is equal to zero.
If you have a complex divisor expression and a NULL result is acceptable, there is an alternative that does not require repeating the divisor expression:
a.CUST_CNT / nullif(b.LIVING_UNIT_CNT, 0)
The NULLIF function returns NULL if the first parameter is equal to the second parameter. In this case it returns NULL if b.LIVING_UNIT_CNT is equal to zero. And anything divided by NULL also becomes NULL. It's a bit of a "trick" maybe, but it saves the expression repetition of the CASE statement.
Try a case statement:
case
when b.LIVING_UNIT_CNT = 0 -- the divisor
then 0 -- a default value
else a.CUST_CNT / b.LIVING_UNIT_CNT
end
ALLRGN_TK_RT_PCT

Can somebody verify this for me in sql or teradata

I have a line of code in Oracle and I had to convert it into Teradata.
The Oracle query is
/* add to avoid invalid number due to junk in column */
AND regexp_instr(table.column, ''[^[:digit:]]'', 1, 1) = 0
The code I have written in Teradata
AND (CASE WHEN (POSITION('' '' IN TRIM(table.column)) > 0) OR (UPPER(TRIM(table.column))
(CASESPECIFIC) <> LOWER(TRIM(table.column)) (CASESPECIFIC))
THEN 1 ELSE 0 end ) = 0
The column is defined as a VARCHAR(20) but I only want to select rows where the data is all numeric. I cannot verify the Teradata query as it is a very long-running query and I don't have access to create tables or rather I can not verify the out put on the database I have. I some how tried and it looks like it works but I once wanted to verify the syntax and my understanding of REGEXP_INSTR.
If I am reading correctly and based on my testing this will break your logic (both return 1):
SELECT (CASE WHEN (POSITION('' '' IN TRIM('1234')) > 0) OR (UPPER(TRIM('1234'))
(CASESPECIFIC) <> LOWER(TRIM('1234')) (CASESPECIFIC))
THEN 1 ELSE 0 END )
SELECT (CASE WHEN (POSITION('' '' IN TRIM('abcd ef1')) > 0) OR (UPPER(TRIM('abcd ef1'))
(CASESPECIFIC) <> LOWER(TRIM('abcd ef1')) (CASESPECIFIC))
THEN 1 ELSE 0 END )
The Teradata Developer's Exchange contains a library of Oracle functions that have been converted to Teradata UDF's that may help you address this problem. With a little effort you could write your own UDF around the isdigit() C function. (isdigit)
It may not be helpful now, but one of the recently announced Teradata 14.0 features is support of regular expressions.
EDIT: Added TD 14 example with REGEXP_INSTR that should solve the problem
SELECT table.column
FROM table
WHERE REGEXP_INSTR(table.column, '[^[digit]])') = 0;

SQL CASE Statement Not Working Correctly

I have a view in SQL Server 2008 with several columns that are expressions of one column divided by another. I have to account for the divisor being 0, so I use a CASE statement. Here is an example of one:
CASE SUM(dbo.GameStats.BringBacksAttempted)
WHEN 0 THEN
0
ELSE
SUM(dbo.GameStats.BringBacks) / SUM(dbo.GameStats.BringBacksAttempted)
END
Even when SUM(BringBacksAttempted) and SUM(BringBacks) are not 0, the statement is always 0. What am I doing wrong?
What data type is BringBacksAttempted and BringBacks?
If both are int and the result comes to be a fraction, you will only see integer part of it.
e.g. 100 / 250 will return 0.
Whereas, CAST(100 as Decimal) / 250 will return 0.40000
Use a CAST or CONVERT on one of the fields.
Try:
CASE
WHEN SUM(dbo.GameStats.BringBacksAttempted) = 0 THEN
0
ELSE
SUM(dbo.GameStats.BringBacks) / SUM(dbo.GameStats.BringBacksAttempted)
END