Why are "Null Like X" and "Not (Null Like X)" equal? - sql

I understand why these 2 statements are false
NULL LIKE 'X'
NULL NOT LIKE 'X'
However, what I don't understand is why these are :
NOT (NULL LIKE 'X')
NOT (NULL NOT LIKE 'X')
For example, these two statements should, I think, return different values :
SELECT CASE WHEN NOT (NULL LIKE 'X') THEN 'True' ELSE 'False' END
SELECT CASE WHEN (NULL LIKE 'X') THEN 'True' ELSE 'False' END

SQL uses a three-valued logic. You say that these are all false:
NULL LIKE 'X'
NULL NOT LIKE 'X'
NOT (NULL LIKE 'X')
NOT (NULL NOT LIKE 'X')
but that's actually not true. They're all null, which is neither true nor false.
A WHEN or WHERE clause rejects non-true values, which means null values as well as false ones, so it may seem like null is the same as false, but as you've noticed, it's not. :-)

NULL is always NULL you cannot compare it to something like that. It cannot be true or false which is what you are looking for as an answer.
It is just like you cannot compare, this will always return zero rows because NULL is an unknown value.
SELECT *
FROM yourTable
WHERE yourCol = NULL

NULL is always undefined.
So the value of
not (NULL like X) is undefined
and also
not (NULL not like X) is undefined
The best approximation of Undefined is NULL so both statements evaluate to NULL (NOT, you understand, equal NULL, we don't know what they equal)

Related

SQL Server Compare to NULL

I have a lot of comparisons that I need to make between a value and its previous value.
For Example: ReceivedBy and PreviousReceivedBy.
I started with:
WHERE ReceivedBy != PreviousReceivedBy
But if either value is null then this returns false, (when I really need it to be true). So I updated it to look like this:
WHERE ReceivedBy != PreviousReceivedBy
OR (ReceivedBy IS NULL AND PreviousReceivedBy IS NOT NULL)
OR (ReceivedBy IS NOT NULL AND PreviousReceivedBy IS NULL)
This works fine, but I have a large list of fields that need to be compared. I would like to find a way to make this comparison with less code (without turning off ANSI_NULLS).
Obviously if there is no other way, then I will just put in all 3 lines for the comparison.
UPDATE:
As an example, here is what I am hoping for
ReceivedBy = 123
PreviousReceivedBy = 123
Result = FALSE
ReceivedBy = 5
PreviousReceivedBy = 123
Result = TRUE
ReceivedBy = NULL
PreviousReceivedBy = 123
Result = TRUE
ReceivedBy = 123
PreviousReceivedBy = NULL
Result = TRUE
ReceivedBy = NULL
PreviousReceivedBy = NULL
Result = FALSE
If both columns are varchars, I'd go with something like this:
coalesce(ReceivedBy, 'NULL') != coalesce(PreviousReceivedBy, 'NULL')
If they are integers, I'd put some values greatly below zero (to distinctly represent null value) instead of 'NULL'.
From names of columns I assume it has to be wether string value or integer value :)
UPDATE
As #Siyual pointed out, replacement string should be "out of the realm of possibility", you should replace 'NULL' above with some non-alphabetical character, as '#' :)
I encountered the same problem with you when taking comparison with nullable value, NULL always returns unknown as far away of our desired only between TRUE or FALSE
I ended up with declare a Scalar-valued functions with these logics like other SQL(s) dealing with null as
Ordinary comparison operators yield null (signifying "unknown"), not
true or false, when either input is null. For example, 7 = NULL yields
null, as does 7 <> NULL. When this behavior is not suitable, use the
IS [ NOT ] DISTINCT FROM constructs:
a IS DISTINCT FROM b => a != b
a IS NOT DISTINCT FROM b => a == b
Which a IS NOT DISTINCT FROM b could be rewritten as
(a IS NOT NULL AND b IS NOT NULL AND a=b) OR (a IS NULL AND b is NULL)
I use sql_variant for these basic parameters: int, datetime, varchar,...
create function IsEqual(
#a sql_variant,
#b sql_variant
)
returns bit
as
begin
return (CASE WHEN (#a IS NOT NULL AND #b IS NOT NULL AND #a=#b) OR (#a IS NULL AND #b is NULL) THEN 1 ELSE 0 END);
end
create function IsNotEqual(
#a sql_variant,
#b sql_variant
)
returns bit
as
begin
return 1-dbo.IsEqual(#a,#b);
end
To use
select dbo.IsEqual(null, null) Null_IsEqual_Null,
dbo.IsEqual(null, 1) Null_IsEqual_1,
dbo.IsEqual(1, null) _1_IsEqual_Null,
dbo.IsEqual(1, 1) _1_IsEqual_1,
dbo.IsEqual(CAST('2017-08-25' AS datetime), null) Date_IsEqual_Null,
dbo.IsEqual(CAST('2017-08-25' AS datetime), CAST('2017-08-25' AS datetime)) Date_IsEqual_Date
For your cases
select dbo.IsNotEqual(123,123) _123_IsNotEqual_123,
dbo.IsNotEqual(5,123) _5_IsNotEqual_123,
dbo.IsNotEqual(Null,123) Null_IsNotEqual_123,
dbo.IsNotEqual(123,Null) _123_IsNotEqual_Null,
dbo.IsNotEqual(Null,Null) Null_IsNotEqual_Null
Another method without munging the data would be to use COALESCE
Where ReceivedBy != PreviousReceivedBy
And Coalesce(ReceivedBy, PreviousReceivedBy) Is Not Null
NULL cannot equal anything, not even another NULL, so if any of the values are NULL, ReceivedBy != PreviousReceivedBy will evaluate as true.
Secondly, if both of the values are NULL, the Coalesce(ReceivedBy, PreviousReceivedBy) Is Not Null will evaluate as false, forcing those to be filtered.
If neither are NULL, the first condition would fail if they are equal.
Admittedly, it’s not saving too much code, but it is an improvement.
This can be easily grouped in parenthesis and copy/pasta’d for all remaining fields you need to check.
Where (ReceivedBy != PreviousReceivedBy And Coalesce(ReceivedBy, PreviousReceivedBy) Is Not Null)
And[Or] (Foo != Bar And Coalesce(Foo, Bar) Is Not Null)
...
WHERE ISNULL(ReceivedBy, -1) != ISNULL(PreviousReceivedBy, -1)
assuming the columns never have negative values
if either value is null then this returns false
It actually returns "unknown" rather than "false" (SQL uses three valued logic). But in a WHERE clause the combined predicates must evaluate to "true" for the row to be returned so the effect here is much the same.
From SQL Server 2022 you can use
WHERE ReceivedBy IS DISTINCT FROM PreviousReceivedBy
(NULLIF(#a, #b) IS NOT NULL) OR (NULLIF(#b, #a) IS NOT NULL) means "#a != #b even if one of them or both are null.

Not Equal logic trouble in SQL Server

I can't believe I'm having so much trouble with this.
Using this statement:
USE XXXX
SELECT
ID, DESCRIPTION, STATUS
FROM
PART
WHERE
PART.ID LIKE 'PCH%'
AND PART.DESCRIPTION NOT LIKE '%OBSOLETE%'
AND PART.STATUS = 'O'
I get a table with 34 entries, each of them containing O in PART.STATUS.
What I actually want to say is, only show me the values which do NOT have a status of O. I know there are other ways around this. values that are not O should be null, but I'm annoyed that I can't figure out how the 'not equal' statement works. When I switch the last line to:
AND PART.STATUS <> 'O'
OR
AND PART.STATUS != 'O'
I get an empty table returned.
If I use the line
AND PART.STATUS IS NULL
I get the table I'm looking for.
What am I misunderstanding about the use of 'not equal statements'?
The problem is 3 valued predicate logic. When at least one side of predicate is NULL the result of predicate is UNKNOWN(no matter you use = or <> or > or <, ...), but WHERE clause only returns rows where predicate evaluates to TRUE. So our job is to make predicate to evaluate to TRUE when PART.STATUS IS NULL. This is done by adding additional check on NULL like:
USE XXXX
SELECT ID, DESCRIPTION,STATUS
FROM PART
WHERE
PART.ID LIKE 'PCH%'
AND PART.DESCRIPTION NOT LIKE '%OBSOLETE%'
AND (PART.STATUS <> 'O' OR PART.STATUS IS NULL)
Here is a little example. Imagine this is your table and you are issuing your statement WHERE PART.STATUS <> 'O'
PART(STATUS)
'A'
'O'
NULL
It evaluates to:
WHERE 'A' <> 'O' --TRUE
WHERE 'O' <> 'O' --FALSE
WHERE NULL <> 'O'--UNKNOWN
Since WHERE clause returns only rows where result of predicate is TRUE, you will get only 'A' here.
the issue here is with the nullvalue since every logical comparation against it would return false for example
PART.STATUS = NULL-- Would be false
PART.STATUS <> NULL-- would also return false
so you should do your comparison like
AND (PART.STATUS <> 'O' OR PART.STATUS IS NULL)
The part you're missing is that NULL is not a value, but the absence of it. A NULL means that the value in that field is either inexistent or unknown. That's why you cant' directly compare a value to a NULL. As stated in other answers, you have to use PART.STATUS IS NULL. Another option would be to use the ISNULL function, wich will test a value for NULL and, if it is, will return whatever value you specify on the second parameter. E.g.
USE XXXX
SELECT ID, DESCRIPTION,STATUS
FROM PART
WHERE
PART.ID LIKE 'PCH%'
AND PART.DESCRIPTION NOT LIKE '%OBSOLETE%'
AND ISNULL(PART.STATUS, '') <> 'O'
Check the documentation for NULL in Sql Server and the ISNULL function. Also, this question could be of use.
USE XXXX
SELECT ID, DESCRIPTION,STATUS
FROM PART
WHERE
PART.ID LIKE 'PCH%'
AND PART.DESCRIPTION NOT LIKE '%OBSOLETE%'
OR PART.STATUS != 'O'
Maybe your misunderstanding is probably about the NULL concept. NULL is not an empty string neither different from 'O', it's just NULL.
That's why you have to use
AND PART.STATUS IS NULL
or
AND isnull(PART.STATUS, '') <> 'O'
It is important to remember how NULL is treated in a database. It isn't a value at all!
No operator (>,<,=.. etc) less (is) used will ever return rows with NULL.
writing in SQL "where column = NULL" is like saying "give me all rows where the value isn't a value
Null values can't be compared with equals(=) or not equals operators. Try the below:
USE XXXX
SELECT ID, DESCRIPTION,STATUS FROM PART WHERE
PART.ID LIKE 'PCH%'
AND PART.DESCRIPTION NOT LIKE '%OBSOLETE%'
AND ISNULL(PART.STATUS, '') <> 'O'

NULL IN (1,2,NULL) returns false

Why does this SQL-statement return 0?
SELECT CASE WHEN NULL IN (9,1,NULL) THEN 1 ELSE 0 END
SQL is based on three-valued logic, where there are three truth values: TRUE, FALSE, and UNKNOWN. The special NULL value is a placeholder for "missing data" and if you compare something, anything, with missing data the result is unknown.
For example, is <missing data> equal to <missing data>? It's impossible to know, so the result is UNKNOWN.
In this particular case, you are trying to find out if <missing data> is in a given list: since the data is missing, it's impossible to know if it's in the list, and the query returns 0.
SELECT CASE WHEN NULL IN (9,1,NULL) THEN 1 ELSE 0 END
I don't know what RDBMS you are using since some of them has configuration on how NULL will be treated.
NULL IN (9, 1, NULL)
can be written as
(NULL = 9) OR (NULL = 1) OR (NULL = NULL)
and not of them were TRUE nor FALSE. They are all NULL. Since there are only two paths in the CASE statement, it falls under ELSE block.
It depends on how NULL is treated in the specific server.
For instance in SQL SERVER you may set ANSI_NULLS off and have it return 1:
SET ANSI_NULLS OFF
SELECT CASE WHEN NULL IN (9,1,NULL) THEN 1 ELSE 0 END
For further info you should read the remarks section of SET ANSI_NULLS
Because you can't compare null values using comparison operators. You can only use is null or is not null or using functions like COALESCE(), ISNULL(). For example
SELECT CASE WHEN COALESCE(NULL,-1) IN (9,1,-1) THEN 1 ELSE 0 END
Null mean not defined.
NUll object is not equal to other NULL
NULL==1 return false
NUll==2 return false
NULL==NULL return also false

TSQL Comparing 2 uniqueidentifier values not working as expected

I'm trying to compare 2 uniqueidentifier values as shown in the query below. However, if one value is null and one value isn't, the result is 'same'?! I'm sure that both values are uniqueidentifiers, and have also tried casting both values to uniqueidentifier to make absolutely sure. The 2 values being compared are coming from different databases with different collations. Does the collation make any difference? Any ideas would be appreciated.
select [result] = case when
[target].StaffID <> [source].StaffID then 'different'
else 'same'
end
from
...
If I replace the <> with an = the query then thinks that 2 null values don't match.
EDIT:
I used:
declare #empty uniqueidentifier
set #empty = '00000000-0000-0000-0000-000000000000'
... isnull(somevalue, #emtpy) <> isnull(othervalue, #empty) ...
NULL is neither equal to something nor equal to nothing. Generally you'd check for null values by comparing with IS NULL. For example,
somefield IS NULL
You could look into using COALESCE for what you're trying to do -- just make sure you use the same data types (in this case UniqueIdentifier):
...
case
when coalesce(t.StaffID,'00000000-0000-0000-0000-000000000000') <>
coalesce(t2.StaffID,'00000000-0000-0000-0000-000000000000')
then 'different'
else 'same'
end
...
http://sqlfiddle.com/#!3/181e9d/1
null is more of an unknown, it's not really a value. Unfortunately SQL will tell you that null = null is false.
Basically you have to cast nulls to empty strings than you can compare. We use IFNULL(value, replacement) to do that...
http://msdn.microsoft.com/en-us/library/ms184325.aspx
Hope this helps.
select case when null = null then 'equal' else 'not equal' end
Above will be "not equal"
select case when ISNULL(null, '') = ISNULL(null, '') then 'equal' else 'not equal' end
This one will be "equal"
And finally your case...
select [result] = case when
ISNULL([target].StaffID, '') <> ISNULL([source].StaffID, '') then 'different'
else 'same'
end
from

Why is there not special handling for CASE WHEN NULL in SQL?

In SQL it is impossible to compare a variable with NULL like this:
CASE x WHEN NULL THEN y ELSE z END
because with the three-valued SQL logic, x = NULL will never return true. One has to do this instead:
CASE WHEN x IS NULL THEN y ELSE z END
This is problematic when x is the result of a procedure that modifies the data for example:
CASE WHEN func(i) IS NULL THEN y
WHEN func(i) = 'a' THEN z
ELSE t
END
So… func(i) will be evaluated twice? Why is there no standard requirement that CASE ... WHEN NULL compares the evaluated expression to NULL with the IS NULL operator?
Are there DBMSes that allow such a construct?
This is just one of the many reasons NULL is evil. One work-around is to choose a magic value as a surrogate for NULL:
CASE COALESCE(func(i), 'nada') WHEN 'nada' THEN y WHEN 'a' THEN z ELSE t END
Because you're not guaranteed that the values after WHEN are just literals, there's no simple way to special case this without introducing more inconsistency, or re-writing SQL so that NULL==NULL.
E.g. if the expression is CASE func(i) WHEN Col1 THEN... and Col1 is null in some rows, but not in others...
I'm not aware of a RDBMS that allows you to to get around this on a case statement.
In Oracle you can swap to the decode function:
decode( func(i), null, 'y', 'a', 'z')
Whilst Null != Null is normal SQL, it is considered equal when using a Decode statement within your logic.