Why does SELECT 'foo' + NULL;
returns NULL
on SQL Server? I would expect it to return foo
.
Update: The SQL query is generated by Entity Framework Core from C# and I expected the null handling behavior to be translated.
Why does SELECT 'foo' + NULL;
returns NULL
on SQL Server? I would expect it to return foo
.
Update: The SQL query is generated by Entity Framework Core from C# and I expected the null handling behavior to be translated.
Well, that's the whole concept of NULL
. It indicates a value that is missing in the database, hence making most operations inapplicable :
Any arithmetic operation on NULL
(addition, substraction, multiplication, division on both ends) returns NULL
.
String concatenation with NULL
returns NULL
(on most RDBMS, but not Oracle, where NULL
is treated as the empty string, hence a query like select NULL || 'A' from dual
returns A
)
Comparison with NULL
(equal, different, like) never match. You usually need to use specific functions such as ISNULL
to test if a value is NULL
For more information please have a look at this Wikipedia page
There are two ways you can handle the concatenation with NULL by setting CONCAT_NULL_YIELDS_NULL as ON or OFF.
PRINT 'Setting CONCAT_NULL_YIELDS_NULL ON';
GO
-- SET CONCAT_NULL_YIELDS_NULL ON and testing.
SET CONCAT_NULL_YIELDS_NULL ON;
GO
SELECT 'abc' + NULL ;
GO
--SET CONCAT_NULL_YIELDS_NULL OFF and testing.
SET CONCAT_NULL_YIELDS_NULL OFF;
GO
SELECT 'abc' + NULL;
GO
When the CONCAT_NULL_YIELDS_NULL is on, it will yield a NULL result, and vice versa.
When this setting is not specified, then the setting of the CONCAT_NULL_YIELDS_NULL database option applies.
To view the current setting for this setting, run the following query
DECLARE @CONCAT_NULL_YIELDS_NULL VARCHAR(3) = 'OFF';
IF ( (4096 & @@OPTIONS) = 4096 ) SET @CONCAT_NULL_YIELDS_NULL = 'ON';
SELECT @CONCAT_NULL_YIELDS_NULL AS CONCAT_NULL_YIELDS_NULL;
NULL
doesn't mean "nothing" or "empty". That's a common misconception. NULL
really means "I don't know". It might be empty... but it might be something else; you just don't know what to put there yet.
When you you combine "foo" with "I don't know", the answer is still "I don't know". Pretty much all databases work the same way; it's what the SQL spec requires.