SQL "NOT IN" Will Fail If The List Contains A Null#

If you are using the NOT IN clause with a SQL Select statement with SQL Server it will not work if there is a null value in the list.  Say for example you want to do:

SELECT * FROM 
Employees 
Where 
Employee.Id NOT IN 
(  
   Select EmployeeId from 
   Salaries 
   where Salary < 100000
)

If the inner select returns a null value then the outer select statement will return no rows.

To try it, just run these 2 SQL statments against any table in your DB.

-- will return rows
SELECT * FROM 
sometable
WHERE ID NOT IN (1,2,3)

-- will return nothing
SELECT * FROM 
sometable
WHERE ID NOT IN (1,2,3, null)
 
Categories:  |  | 
Monday, October 29, 2007 12:02:18 PM (Central Standard Time, UTC-06:00) #    Comments [0]  | 

 

Name
E-mail
(will show your gravatar icon)
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

All content © 2008, Christopher May, Inc
Open Job Positions
On this page
Google Ads
This site
Calendar
<November 2008>
SunMonTueWedThuFriSat
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456
Archives
Sitemap
Blogroll OPML
Disclaimer

Powered by: newtelligence dasBlog 1.9.6264.0

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Send mail to the author(s) E-mail

Theme design by Jelle Druyts


Pick a theme: