Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NULL values are excluded. Why?

Tags:

This is about a bizarre behaviour I found in Microsoft Sql Server. Please correct me if I'm wrong.

SELECT COUNT(*) FROM TABLEA  WHERE [Column1] IS NULL; 

This returns 30018 rows.

CREATE VIEW VIEWB AS  SELECT * FROM TABLEA AS t1  WHERE t1.[Column1] NOT IN ('Cross/Up sell', 'Renegotiation', 'Renewal')   

If I check VIEWB, I don't find NULL in Column1:

SELECT COUNT(*) FROM VIEWB  WHERE [Column1] IS NULL; 

This returns 0 rows.

Why? The query above excludes the 3 values, but it isn't supposed to exclude NULL. Why does Ms Sql Server behave this way? Should I have expected this? How can I fix it?

like image 446
Sam Avatar asked Jul 15 '12 12:07

Sam


People also ask

How do you exclude NULL values?

Hiding null values To filter null dimensions or discrete measures, drag the pill to the Filter shelf and deselect Null. The null value will appear in the list with discrete values, where you can then remove it. When a measure contains null values, they are usually plotted in a view as zero.

Why should NULL values be avoided?

They should be avoided to avoid the complexity in select & update queries and also because columns which have constraints like primary or foreign key constraints cannot contain a NULL value.

Why NULL is not allowed in primary key?

Primary key constraints NULL values are not allowed. If the column(s) contain NULL values, the system will not add the primary key constraint. See ALTER TABLE statement for more information. A table can have at most one PRIMARY KEY constraint.

In which function NULL values are excluded from the result returned?

Aggregate function (like Count(), Sum(), Avg()) ignore null values.


2 Answers

This is actually a common mistake made with SQL Server in treating NULL as a value. By default, it's treated as UNKNOWN, as documented here. So, in your view, you also need to include an OR t1.[Column1] IS NULL.

You can change this behavior by calling SET ANSI_NULLS OFF. It is not recommended to use this, however, as the feature is deprecated as pointed out by @Martin Smith.

This is not a SQL Server specific issue, however. It's part of the ANSI SQL standard.

like image 198
Sumo Avatar answered Oct 07 '22 15:10

Sumo


SQL uses three valued logic.

t1.[Column1] NOT IN ('Cross/Up sell', 'Renegotiation', 'Renewal')  

is equivalent to

t1.[Column1] <> 'Cross/Up sell' AND   t1.[Column1] <> 'Renegotiation' AND  t1.[Column1] <>  'Renewal') 

When t1.[Column1] is NULL this expression evaluates to UNKNOWN rather than TRUE so these rows are not returned.

The only time NULL NOT IN ( ... ) will be returned is if the NOT IN clause evaluates to an empty set.

like image 31
Martin Smith Avatar answered Oct 07 '22 15:10

Martin Smith