 |
| SQL Server 2005 General discussion of SQL Server *2005* version only. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the SQL Server 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

September 25th, 2008, 10:10 AM
|
|
Authorized User
|
|
Join Date: Nov 2005
Posts: 44
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
IN and NULLs
I use NorthWind database. Why result is same(none)?
SELECT * FROM Customers WHERE Country NOT IN (null)
SELECT * FROM Customers WHERE Country IN (null)
|
|

September 25th, 2008, 01:22 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
http://msdn.microsoft.com/en-us/library/ms177682.aspx
I quote:
"Caution:
Any null values returned by subquery or expression that are compared to test_expression using IN or NOT IN return UNKNOWN. Using null values in together with IN or NOT IN can produce unexpected results."
If you read the docs on the = and <> operators, you can see why.
|
|

September 26th, 2008, 03:38 AM
|
|
Authorized User
|
|
Join Date: Nov 2005
Posts: 44
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Yes results are strange
SELECT * FROM Customers WHERE Country IN ('Germany',null)
SELECT * FROM Customers WHERE Country NOT IN ('Germany',null)
|
|

September 26th, 2008, 04:07 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
Yes, you can't do that.
You need to do something like this:
SELECT * FROM Customers WHERE ISNULL(Country,'Germany') = 'Germany'
SELECT * FROM Customers WHERE ISNULL(Country,'Germany') <> 'Germany'
|
|

September 26th, 2008, 10:40 PM
|
|
Friend of Wrox
|
|
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
|
|
Wrapping a column name in the WHERE clause in a function in the presence of indexes will prevent an index seek causing the code to slow down quite a bit. Better to use an "OR".
--Jeff Moden
|
|

September 27th, 2008, 01:58 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
Good point.
Though I'm surprised that SQL Server doesn't handle ISNULL( ) as a special case. In fact, I give the SQL Server compiler people big minus points for not doing so, since it's such an obvious and easy optimization to make. [Assuming that you are right and they don't, that is.]
************
Joxa: What Jeff is saying is that you should code that as
SELECT * FROM Customers WHERE Country IS NULL OR Country = 'Germany'
SELECT * FROM Customers WHERE Country IS NOT NULL AND Country <> 'Germany'
Assuming that the Country field is indexed. (Won't matter much, if it's not.)
|
|

September 27th, 2008, 08:12 AM
|
|
Friend of Wrox
|
|
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
|
|
Quote:
quote:Originally posted by Old Pedant
Assuming that the Country field is indexed. (Won't matter much, if it's not.)
|
Actually, it does matter... if the proper indexing is missing, the ISNULL method actually works a bit faster than the OR.
I agree... stupid optimizer. :D
Personally, I'd use the OR method either way because there's always a chance that someone (or me) could add the proper indexing. If you use ISNULL, indexing will only help a tiny bit.
--Jeff Moden
|
|

September 29th, 2008, 01:00 AM
|
|
Friend of Wrox
|
|
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
No need for IS NOT NULL here
SELECT * FROM Customers WHERE <s>Country IS NOT NULL AND</s> Country <> 'Germany'
|
|

September 29th, 2008, 03:03 AM
|
|
Authorized User
|
|
Join Date: Nov 2005
Posts: 44
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thank you guys.:D
|
|
 |