Solved

working with NULL values

Posted on 2010-11-24
5
248 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 39

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now