Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

working with NULL values

Posted on 2010-11-24
5
Medium Priority
?
300 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 1000 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 1000 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 41

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 500 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 500 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

879 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