Count null values using CASE WHEN [duplicate] - sql

This question already has answers here:
SQL is null and = null [duplicate]
(4 answers)
Closed 8 years ago.
I am trying to count the number of missing postal codes using CASE to flag null values as 1, otherwise 0.
CASE WHEN ([postcode]=NULL)
THEN 1
ELSE 0 END AS pc_missing
Even though I know there are lots of missing data, none of them receive the value of 1.
What have I done wrong?

null is not a value - it's the lack of a value. null returns "unknown" (which is not true) when evaluated against any value operator - =, !=, >, <, etc. Instead, you should check it with the is [not] null operator:
CASE WHEN ([postcode] IS NULL)
THEN 1
ELSE 0 END AS pc_missing

Related

SQL Case statement - Null + Not Null - then return a column [duplicate]

This question already has answers here:
SQL is null and = null [duplicate]
(4 answers)
Closed 11 months ago.
I use SQL inside a 3rd party system (So don't know the type it is)
I am trying to make a CASE work on a column using data from 2 more.
I want to display a column call Channel that is calculated using the following logic:
If column O.Test is blank and column o.subsource is not blank, display 'RESEND', otherwise display the value of column o.Source.
This is part of the SQL showing the CASE I wrote to do this:
select
-- other columns
(CASE
WHEN o.Test = NULL AND o.Subsource IS NOT NULL THEN 'RESEND'
ElSE o.Source
END) o.Source AS 'Channel',
-- other columns
The SQL runs with no errors but the output always shows what is in o.Source.
X = NULL is not equal IS NULL X
Check this question,
SQL is null and = null
change this statement "o.Test = NULL" instead of "o.Test IS NULL"

MsSql behaves differently when filtering for an empty string by positive and negative query [duplicate]

This question already has answers here:
Issues with SQL comparison and null values
(8 answers)
Closed 2 years ago.
I just realized very strange behavior of my MsSql database when filtering empty string != '' or <> '' regarding NULL values.
I have following data in the table
ID
Value
(table X)
1
(empty string)
2
NULL
(no value)
3
text
(some real text)
This query select * from X where Value = '' results in:
ID: [1]
Both queries select * from X where Value != '' and select * from X where Value <> '' result in:
ID: [3]
What I do not understand is, why 2nd query is not returning ID=2?
I know the syntax for checking explicitly on null values where Value IS NULL so I would expect, that 2nd query would behave differently. When checking for non-empty values, I used to write where Value <> '' AND Value IS NOT NULL. From now on I am totally confused...
NULL does not have a string value - or any value at all, whereas ' ' is an empty string. You're looking for not empty strings in your 2nd query. NULL is not an empty string or a string at all. NULL is a "value" of no other type than NULL itself
It is not possible to test for NULL values with comparison operators,
such as =, <, or <>.
https://www.w3schools.com/sql/sql_null_values.asp

Initialize number by NULL [duplicate]

This question already has answers here:
What is NULL in SQL?
(5 answers)
Closed 4 years ago.
What is the difference between initializing a number type variable by null and by Zero in PLSQL?
example-
l_person_id NUMBER :=NULL;
and
l_person_id NUMBER :=0;
Does this effect anywhere in a code?
There is a difference.
If you have comparison operations in your code then you can get unexpected behaviour:
5 > l_person_id_null -- false
5 > l_person_id_0 -- true
Also, with NULL you'll have to use IS NULL and IS NOT NULL operators instead of =.

Output of "select 1 where NULL <> -1;" [duplicate]

This question already has answers here:
why is null not equal to null false
(7 answers)
Closed 6 years ago.
I guess I don't fully understand the meaning of NULL in SQL. I ran this SQL and I expected to see 1 as the output but I didn't see that:
select 1 where NULL <> -1;
Isn't NULL and -1 different? Can anyone explain why this clause of "NULL <> -1" is FALSE?
NULL Is not a value and therefore cannot be compared with any other value and get any other result than null. Docs. And how to compare nulls.
NULL means unknown. So with where NULL <> -1 you want to know whether the unknown value equals -1. The DBMS does not know (of course), so the result of the expression is neither TRUE nor FALSE; it is NULL.
Only rows for which the WHERE clause results in TRUE are selected. As your expression doesn't result in TRUE but in NULL, there is no row selected.

difference between <> and != [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Oracle <> , != , ^= operators
in sql, what is the difference between <> and !=, we can use both for "NOt Equal to".
is there any difference in between them?
ex.
select * from student where no != 2;
&
select * from student where no <> 2;
is any advantage of using one insted of another?
What are the main factors because of which != is not made as ISO standard
For SQL Server:
They are the same. Both are two Not Equal To operators. But != is not ISO standard, as quoted from Comparison Operators:
<> (Not Equal To) Not equal to
!= (Not Equal To) Not equal to (not ISO standard)