-
Guoxiong 8,076 Reputation points
2021-03-01T15:51:31.107+00:00 You can not use a equal sign (=) to compare values between two fields if one or two fields may have NULL value. Try this:
DECLARE @s1 varchar(10) = NULL; DECLARE @s2 varchar(10) = NULL; -- Incorrect: Return nothing IF @s1 = @s2 BEGIN SELECT 1; END -- Correct: Return 1 IF ISNULL(@s1, '') = ISNULL(@s2, '') BEGIN SELECT 1; END -- Correct: Return 1 IF @s1 IS NULL AND @s2 IS NULL BEGIN SELECT 1; END
From w3schools, It is not possible to test for NULL values with comparison operators, such as =, <, or <>.
-
Peter Jones (BIDA) 81 Reputation points
2021-03-01T19:10:21.587+00:00 Hi Guoxiong ,
Strange. I have never heard of such a restriction and I have been dealing with relational databases nearly 40 years.
I tested this statement and it works.
So the equality test works when done as a case statement. It is likely defaulting to 0 for the else.
I am going to test on some other databases and see what they do.
Best Regards
Peter
or not ((1 = ( case when (zxt_dnn_sitelog_src.datetime = zxt_dnn_sitelog_m1.datetime) then 1 else 0 end )) or ( zxt_dnn_sitelog_src.datetime is null and zxt_dnn_sitelog_m1.datetime is null))
-
SM 16 Reputation points
2021-03-01T21:13:41.567+00:00 Odd that you have not discovered in 40 years that NULL means unknown and you cannot logically compare any unknown value to anything. In your example, the first comparison returns UNKNOWN if either column is null so the ELSE part of the case expression is returned. FWIW it is WAY past time to stop using old-style joins.
-
Peter Jones (BIDA) 81 Reputation points
2021-03-02T13:23:16.253+00:00 Hi SM,
it's because other databases resolve the query correctly.
I had a friend test on DB2 UDB and it resolves the query properly.
So I am testing on some other databases to see what it does on those databases.
With respect to NULLS?
Yes I am well aware of null arithmetic.
Most databases do not allow a comparison operator to resolve to "unknown".
They resolve to true or false.
When in doubt they resolve to false.
On the old join style?
Sure, there is a new join style. So?
The generated code comes from an ETL tool a pal of mine wrote.
The ETL software is free and open source so anyone can update it.
http://www.instantbi.com/freebies/downloads/
In other areas of the generated code it uses the new join functions for such things as left joins.
The much more important feature is this ETL tool cuts ETL development time by 50% or more.
Even if you go into production with Informatica/SSIS etc.
I wish you a good day!
-
Dan Guzman 7,306 Reputation points
2021-03-02T13:53:31.3+00:00 Most databases do not allow a comparison operator to resolve to "unknown".
All ISO SQL standard compliant relational databases must adhere to 3-value results TRUE, FALSE, UNKNOWN) for comparison operator results. NULL predicates (IS NULL or IS NOT NULL) are a pain but blame E. F. Codd.
Below is an excerpt from the DB2 LUW doc:
Two strings are equal if they are both empty or if all corresponding bytes are equal. If either operand is null, the result is unknown.
-
Peter Jones (BIDA) 81 Reputation points
2021-03-03T13:23:57.587+00:00 Hi Dan,
that's interesting because I had a friend test on DB2 and it processed the query as is properly.
Perhaps there is an option to turn on this feature?
I put up a comment to Tom mentioning that this behaviour has been standard since the 80s.
So perhaps many databases support the old way as an option.
But good for me to learn that this is how they are able to resolve this now. :-)
Best Regards
Peter
-
Peter Nolan 1 Reputation point
2021-03-06T11:44:08.33+00:00 Hi SM,
I am the guy Peter Jones is talking about.The software he is using is a version of my software. There are many people using versions of my software now.
And yes, he is correct. I had not seen that behaviour in databases either and I WROTE a relational database back in 1985! LOL!
Back then the people writing RDBMSs had the discussion how (null field = not null field) should evaluate since we could not call it "null" which is what relational theory says it should be. There is a difference between "theory" and "the real world".
So yes, I was also surprised to see this behaviour. Ok?
Best Regards
Peter
Sign in to comment -
You can not use a equal sign (=) to compare values between two fields if one or two fields may have NULL value. Try this:
DECLARE @s1 varchar(10) = NULL;
DECLARE @s2 varchar(10) = NULL;
-- Incorrect: Return nothing
IF @s1 = @s2
BEGIN
SELECT 1;
END
-- Correct: Return 1
IF ISNULL(@s1, '') = ISNULL(@s2, '')
BEGIN
SELECT 1;
END
-- Correct: Return 1
IF @s1 IS NULL AND @s2 IS NULL
BEGIN
SELECT 1;
END
From w3schools, It is not possible to test for NULL values with comparison operators, such as =, <, or <>.