Solved

working with NULL values

Posted on 2010-11-24
5
282 Views
Last Modified: 2012-05-10
what is the reason that the select statement does not bring back valid records. seems like the NULL can kill the query from bringing back good results..
-----------------------------------

create table one (first tinyint)
insert into one select 1 union select 2 union select 3
create table two (second tinyint)
insert into two select 1 union select NULL

select first from one where first not in (select second from two) -- does not work

drop table one
drop table two
0
Comment
Question by:anushahanna
5 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 250 total points
ID: 34208541
That is because NULLS are a quirk, they are incomparable.
set @d = null
if @d = 1 print 1
if @d != 1 print 1
neither will fire because it is neither equal to nor not equal to

It is just what it is
For IN (.. including NULL ..) it will return matches EXCEPT null, i.e. null records from left hand side won't be matched [unless ANSI_NULLS is set off]

FOR NOT IN (..includes null)
It will never return anything.
0
 
LVL 58

Assisted Solution

by:cyberkiwi
cyberkiwi earned 250 total points
ID: 34208615
For not in (... includes null)
It will never return anything. -- except when ansi_nulls is off

good programming practice indicates nulls should never be in an -in- list, i.e. filter it out

in (select i from tbl where i is not null)
0
 
LVL 40

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 125 total points
ID: 34208623
could also do:

select first from one where first not in (select isnull(second,'') from two)

0
 
LVL 23

Assisted Solution

by:Racim BOUDJAKDJI
Racim BOUDJAKDJI earned 125 total points
ID: 34209514
NULL are not a value but a marker for missing value.  The best way to do design is to avoid using them at any cost.

SELECT * FROM SOMETABLE WHERE NULL=NULL
produces the same as
SELECT * FROM SOMETABLE WHERE NULL<>NULL

0
 
LVL 6

Author Comment

by:anushahanna
ID: 34210242
phew.. will be careful with that one.. thanks for the example to make it easy..

really appreciate it..
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question