SQL handling null values - sql

The issue is i have 3 conditions good bad ugly. My goal is if one of the following condition is null then replace null to 0 else if all the conditions are null then show it as null
case when 'Good' is not null or 'Bad' is not null or 'Ugly' is not null
then coalesce(value,0)
else value
end result
The problem here is even with the condition it turns all the nulls to 0 which is not what i want. Thanks in advance

Your current expression says (in pseudocode):
IF <something is true>
IF value IS NULL
RETURN 0
ELSE
RETURN value
ENDIF
ELSE
RETURN value
ENDIF
Is that what you wanted?

I think maybe you want something like this. If I get more details I will update.
If I were just going off of this comment
the question is if good is null and bad and ugly for the same product
has a value 0-100 then i want the null to be replaced to 0. if all the
three are null for the same prod then i want them to be nulls and not
0
I would give you something like this
case
when good is not null then good
when good is null and coalesce(bad,-1) between (0 and 100) and coalesce( ugly,-1) between (0 and 100) then 0
else null
end
If you were trying to provide all 3 columns (I am assuming you have multiple columns) you might do something like this.
select
case
when good is null and bad is null and ugly is null then null
when good is null and (bad is not null or ugly is not null) then 0
else good
end as 'good column values'
,
case
when good is null and bad is null and ugly is null then null
when bad is null and (good is not null or ugly is not null) then 0
else bad
end as 'bad column values'
,
case
when good is null and bad is null and ugly is null then null
when ugly is null and (good is not null or bad is not null) then 0
else ugly
end as 'ugly column values'

Your code says "if one of good, bad, or ugly is not null" but your question says "if one of good, bad, or ugly is null"
If I'm understanding you correctly, just take out the nots.
Edit: I'm also assuming that you don't actually mean to use the literal strings "Good", "Bad" and "Ugly", but that they are meant to represent column names or something.
You should be aware that using these strings means that your code will never enter the "else" condition, as "Good", "Bad" and "Ugly" are not null.
EDIT 2: take your column names (good, bad, ugly) out of quotes so they will actually be evaluated. This is why you're still seeing that error.

Related

SQL case expression with where condition

how can I arrange the case expression of sql in the correct format as I am getting errors.
I am trying to make a condition if the field date of leaving is not null display left otherwise display active
SELECT DISTINCT
PEO.PEOPLE_ID AS [PEOPLE_ID]
,PEO.EMPLOYEECLOCKNO AS [EMPLOYEENUMBER]
,ISNULL(CONVERT(VARCHAR(50),PEO.DATEOFJOIN,103),'') AS [DATEOFJOIN]
--,ISNULL(PEO.DISPLAYSTATUS,'') AS [DISPLAYSTATUS]
,CASE
When ISNULL(PEO.STATUS,'' ) = 'A'
Then 'ACTIVE'
ELSE 'LEFT' WHERE PEO.DATEOFLEAVING <> NULL
END as [STATUS]
INTO #BSP_EMPLOYEEDETAILS
Thank you for your help.
I am trying to make a condition if the field date of leaving is not null display left otherwise display active
CASE WHEN PEO.DATEOFLEAVING IS NULL THEN 'ACTIVE' ELSE 'LEFT' END
(I prefer to make boolean checking positive-feeling rather than negative-feeling, but if you want it negative...)
CASE WHEN PEO.DATEOFLEAVING IS NOT NULL THEN 'LEFT' ELSE 'ACTIVE' END
Note well HoneyBadger's comment; you should only compare something to null using IS NULL or IS NOT NULL - the result of a null right operand with any non-IS operator, is null:
NULL IS NULL --true
NULL IS NOT NULL --false
NULL = NULL --null, which eventually will (probably) be processed as false
NULL <> NULL --null, which eventually will (probably) be processed as false
NULL != NULL --null, which eventually will (probably) be processed as false
NOT(NULL = NULL) --null, which eventually will (probably) be processed as false
I say "eventually" because it's important; null spreads. If you look at the last case, NULL=NULL is NULL, which then becomes NOT(NULL) which is also NULL. If NULL=NULL instantly became FALSE then NOT(NULL=NULL) would be NOT(FALSE) which would be TRUE. The fact that the null spreads throughout the entire set of operations you subject it do, popping out at the end and then being interpreted as false is something that may be advantageous, but always something you should be aware of...
Of course if the thing that receives it is intended for processing NULL, like COALESCE(NOT(NULL=NULL), 'it was null') then it will receive a null (and in this case would output 'it was null') but generally when youre using it in a boolean context like CASE WHEN NOT(mycolumn=NULL) THEN... or WHERE NOT(mycolumn=NULL) the NULL it becomes is received by case when/where and interpreted as false.
It's probably worth pointing out that SQLServer doesn't support booleans in these sort of contexts so you wouldn't get away with using a coalesce like that, or even the NOT(NULL=NULL) example but if you want to experiment to sound out your knowledge, MySQL is a lot more forgiving, and will allow queries like:
select case when (NOT(x = NULL)) IS NULL THEN 't' ELSE 'f' end
from (select null as x)y
select case when (NOT(x = NULL)) IS UNKNOWN THEN 't' ELSE 'f' end
from (select null as x)y
SQLServer expects you to make your null determinations sooner, and doesn't let you bandy around with things that it can see have become null/unknown
Footnote about UNKNOWN; it's generally treated (in the spec, even) as synonmous with a boolean NULL - it's talked about in the truth table sense as the third value in a tri-state boolean logic, but DBs generally make for interchangeable use if they support UNKNOWN as a keyword. For me, personally, I like one-less-thing-to-remember; you can learn how NULL behaves and achieve logically consistent results by applying those simple rules: IS NULL, IS NOT NULL, anything else becomes NULL which then nulls everything it touches

Check column IS NULL not working in Case Expression

I am trying to check a column which belongs to View "IS NULL" and it works good with select statement:
SELECT TOP (1000) [Invoice_Number]
,[Invoice_Date]
,[Invoice_Amount]
,[Invoice_CoCd]
,[Invoice_vendor]
,[Invoice_PBK]
,[Invoice_DType]
,[Invoice_DueDate]
,[Invoice_ClgDate] FROM [dbo].[viewABC] where Invoice_PBK IS NULL
Now I have an update statement, where I need to update a column in a table based on NULL in VIEW:
UPDATE cis
SET
cis.InvoiceStatus =
(
CASE
WHEN RTRIM(LTRIM(imd.[Invoice_PBK])) IS NULL THEN
'HELLO'
WHEN RTRIM(LTRIM(imd.Invoice_DType)) = 'RD'
THEN '233' END)
FROM
[dbo.[tblABC] cis,
[dbo].[viewABC] imd
WHERE [condition logic]
These is no issue with where condition, the IS NULL in the CASE expression causing the problem.
Can someone help please?
You may want to just test your Logic piece by piece.
Try
SELECT ISNULL(RTRIM(LTRIM(imd.[Invoice_PBK])),'Hey Im Null')
FROM
[dbo].[tblABC] cis,
[dbo].[viewABC] imd
WHERE [condition logic]
See if you get that String value on your returned column(s) from the Query. Then, build from there. It could be something as simple as the JOIN. Which I'm not a fan of that old syntax but, without more info on this table other than [conditions]. It's hard to just guess an answer for you. You have no detailed evidence that helps us. It very well could be your conditions but, you're saying "condition logic" and that does nothing for the group on this thread.
Looking at this expression:
cis.InvoiceStatus =
CASE
WHEN RTRIM(LTRIM(imd.[Invoice_PBK])) IS NULL THEN
'HELLO'
WHEN RTRIM(LTRIM(imd.Invoice_DType)) = 'RD' THEN
'233'
END
And this symptom:
CIS.InvoiceStatus gets updated with NULL
The obvious conclusion is neither WHEN condition is met, and therefore the result of the CASE expression is NULL.
Maybe you wanted this, which will preserve the original value in that situation:
cis.InvoiceStatus =
CASE
WHEN RTRIM(LTRIM(imd.[Invoice_PBK])) IS NULL THEN
'HELLO'
WHEN RTRIM(LTRIM(imd.Invoice_DType)) = 'RD' THEN
'233'
ELSE
cis.InvoiceStatus
END
Or maybe you wanted this, to also match an empty string value:
cis.InvoiceStatus =
CASE
WHEN NULLIF(RTRIM(LTRIM(imd.[Invoice_PBK])),'') IS NULL THEN
'HELLO'
WHEN RTRIM(LTRIM(imd.Invoice_DType)) = 'RD' THEN
'233'
END
It's also worth pointing out the two WHEN conditions are looking at two different columns.
Finally, it may be worth a data clean-up project here. Needing to do an LTRIM() will break any chance of using indexes on those fields (RTRIM() is slightly less bad), and index use cuts to the core of database performance.

IS NULL doesn't work

I have a table like the one above with the two left columns (Both of them are integer) and I added to this table two more fields:
Table1:
Asset_Value Contract_Value
-------------------------------
0 NULL
NULL 200
0 NULL
And the query:
Select
Asset_Value, Contract_Value,
Case
when Asset_Value is null
then 1
else 0
end As Ind_ForNullA,
Case
when Contract_Value is null
then 1
else 0
end As IndForNullC
from
table1
However, I get strange results:
Asset_Value Contract_Value Ind_ForNullA IndForNullC
----------------------------------------------------
0 NULL 1 1
NULL 200 0 0
0 NULL 0 1
Update : Never Mind. Damm comma has been forgetten.
Try with ' ' empty string instead of null.
Try to use the function ASCII
ASCII ( character_expression )
Using this function you can understand what is the really character in the column Asset_Value: for example NULL value must have ASCII code: '00' Table with character ASCII code
Why are you bothering to define something as NULL--which is appropriate--but, in this case or that one, changing it to one or zero (an attempt at Boolean, I imagine)--which is inappropriate?
What will you do next: decide that, in a third report, you want to have a REAL boolean that displays TRUE or FALSE?
Go with NULL consistently, throughout the entire application, everywhere, or don't. However, DO NOT MIX AND MATCH PARADIGMS and expect consistent results. Also, try not to rely upon CASE: there's very, very legitimate reason to depend upon that--especially for something as simplistic as what you're doing.
FYI, the only reason I can conceive why you would want this "indicator" field is so that you can test whether INDICATOR_A = 1. However, since you can test ASSET IS NULL, why even bother? NEVER introduce extraneous mechanisms when there is no overpowering reason to do so.

Sql Server: CASE Statement does unexpected behavior when comparing to NULL

Given:
The following Select statement:
select case NULL
when NULL then 0
else 1
end
Problem:
I'm expecting this to return 0 but instead it returns 1. What gives?
Generally speaking, NULL is not something you should attempt to compare for equality, which is what a case statement does. You can use "Is NULL" to test for it. There is no expectation that NULL != NULL or that NULL = NULL. It's an indeterminate, undefined value, not a hard constant.
-- To encompass questions in the comments --
If you need to retrieve a value when you may encounter a NULL column, try this instead:
Case
When SomeColumn IS NULL
Then 0
Else 1
End
I believe that should work. As far as your original post is concerned:
Select Case NULL
When NULL then 0 // Checks for NULL = NULL
else 1 // NULL = NULL is not true (technically, undefined), else happens
end
The trouble is that your Case select automatically attempts to use equality operations. That simply doesn't work with NULL.
I was going to add this as a comment to Aaron's answer, but it was getting too long, so I'll add it as another (part of the) answer.
The CASE statement actually has two distinct modes, simple and searched.
From BOL:
The CASE expression has two formats:
The simple CASE expression compares an expression to a set of simple expressions to determine the result.
The searched CASE expression evaluates a set of Boolean expressions to determine the result.
When the simple CASE (your example) does what it describes as comparison it does an equality comparison - i.e. =
This is clarified in the later documentation:
The simple CASE expression operates by comparing the first expression
to the expression in each WHEN clause for equivalency. If these
expressions are equivalent, the expression in the THEN clause will be
returned.
Allows only an equality check.
Because anything = NULL is always false in ANSI SQL (and if you didn't know this, you need to read up on NULLs in SQL more generally, particularly also with the behavior in the other searched comparison - WHERE x IN (a, b, c)), you cannot use NULL in a simple case and have it ever be compared to a value, with a NULL either in the initial expression or in the list of expressions to be compared against.
If you want to check for NULL, you will have to use an IF/ELSE construct or the searched CASE with a full expression.
I agree that it's kind of unfortunate there is no version which supports an IS comparison to make it easier to write:
select case colname
when IS NULL then 0
else 1
end
Which would make writing certain long CASE statements easier:
select case colname
when IS NULL then ''
when 1 then 'a'
when 2 then 'b'
when 3 then 'c'
when 4 then 'd'
else 'z'
end
But that's just wishful thinking...
An option is to use ISNULL or COALESCE:
select case COALESCE(colname, 999999) -- 999999 is some value never used
when 999999 then ''
when 1 then 'a'
when 2 then 'b'
when 3 then 'c'
when 4 then 'd'
else 'z'
end
But it isn't always a great option.
In addition to the other answers, you need to change the syntax for CASE slightly to do this:
SELECT CASE
WHEN NULL IS NULL THEN 0
ELSE 1
END;
Using the value in your syntax implicitly uses an equals comparison. NULL is unknown, and so is NULL = NULL, so with your current code you will always get zero 1 (geez I did it too).
To get the behavior you want, you can use SET ANSI_NULLS ON; however note that this can change other code in ways you may not be able to predict, and the setting is deprecated - so it will stop working at all in a future version of SQL Server (see this SQL Server 2008 doc).
You need to use the IS NULL operator. Standard comparison operators do not work with NULL.
Check out these MSDN articles about Null that may be useful:
IS [NOT] NULL (Transact-SQL)
Null Values

Addition with NULL values

In a stored procedure (Oracle in my case), I want to add some values to an existing record. Problem is that both the existing value and the value to be added can be null. I only want the result to be NULL when both operands are null. If only one of them is null, I want the result to be the other operand. If both are non-null, I want the result to be "normal" addition.
Here's what I am using so far:
SELECT column INTO anz_old FROM aTable Where <someKeyCondition>;
IF anz_old IS NULL
THEN
anz_new := panzahl;
ELSE
anz_new := anz_new + NVL (panzahl, 0);
END IF;
UPATE aTabel set column = anz_new Where <someKeyCondition>;
Is there a more elegant way (pereferably completely in SQL, i.e. just in an update statement short of a long CASE-Statement with basically the same logic as the above code)?
If you want to add a and b and either may be null, you could use coalesce, which returns the first non-null parameter you pass it:
coalesce(a+b, a, b)
So in this case, if neither parameter is null, it will return the sum. If only b is null, it will skip a+b and return a. If a is null, it will skip a+b and a and return b, which will only be null if they are both null.
If you want the answer to be 0 rather than null if both a and b are null, you can pass 0 as the last parameter:
coalesce(a+b, a, b, 0)
Do consider #erwins answer - null might not be the right thing to be using.
I accomplished it this way:
coalesce("Column1",0.00) + coalesce("Column2",0.00)
I'm working with front end high level execs.... They don't understand why NULL and 0 aren't handled the same way.
In my case it works, just replacing NULLs with 0.00... may not in all though :)
You can also use ISNULL, so if you have 3 values
isnull(val1,0)+isnull(val2,0)+isnull(val3,0)
which ever column will have a NULL will use a 0, otherwise its original value.
In SQL, Null is supposed to be a state that says "I don't know".
If you don't know how much b is, then you also do not know how much a+b is, and it is misleading to pretend that a+b=a in that case.
In SQL terms, when adding numbers, a result of NULL means there were no non-null numbers added.
This suggests that a sensible answer in SQL terms would be
CASE WHEN A IS NULL AND B IS NULL THEN NULL ELSE ISNULL(A, 0) + ISNULL(B, 0) END