Solved

working with NULL values

Posted on 2010-11-24
5
291 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: 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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

627 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