dbTalk Databases Forums  

behavior of in operator with null values

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss behavior of in operator with null values in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
S.S.
 
Posts: n/a

Default behavior of in operator with null values - 12-02-2009 , 06:39 PM






I don't quite understand the expected behavior of the in operator when
used with a list that contains the null value. The following is some
short sample code, which returns an empty result set. My expection
would be a one row result set:


create table TestTable (
TestColumn nvarchar(10)
)

insert into TestTable (TestColumn) values ('Test')

select * from TestTable where TestColumn not in (null)




Obviously this is a silly example but scales to any list that is a
query that could return null.

Can anyone clarify, or point me to some good docs on the behavior of
the in operator? BOL didn't seem to have anything specific on this
behavior.

TIA.

Reply With Quote
  #2  
Old   
Serge Rielau
 
Posts: n/a

Default Re: behavior of in operator with null values - 12-02-2009 , 07:14 PM






S.S. wrote:
Quote:
I don't quite understand the expected behavior of the in operator when
used with a list that contains the null value. The following is some
short sample code, which returns an empty result set. My expection
would be a one row result set:


create table TestTable (
TestColumn nvarchar(10)
)

insert into TestTable (TestColumn) values ('Test')

select * from TestTable where TestColumn not in (null)
Let's rewrite the query:
SELECT * FROM TestTable WHERE NOT (testColumn = NULL)

A row is returned if the WHERE clause is TRUE
In order for this to be TRUE testColumn = NULL must be FALSE

However 'Test' = NULL is UNKNOWN and which is different from FALSE
That is why there is an IS NULL predicate.

Here is a treatise on the dilemma of NULL:
http://www.dbdebunk.com/page/page/1706814.htm

Cheers
Serge
--
Serge Rielau
SQL Architect DB2 for LUW
IBM Toronto Lab

Reply With Quote
  #3  
Old   
Plamen Ratchev
 
Posts: n/a

Default Re: behavior of in operator with null values - 12-02-2009 , 07:35 PM



Take a look at the first case in this article, it explains the issue in detail with different methods to handle it:
http://www.simple-talk.com/sql/t-sql...ming-mistakes/

--
Plamen Ratchev
http://www.SQLStudio.com

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.