Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Using <> with NULL's in Column

Author  Topic 

itsmarkdavies
Starting Member

14 Posts

Posted - 2007-02-28 : 05:47:25
Field1 in Table Contractors can contain either 'A', 'I' or NULL.
The following SQL does NOT return NULL values :-

SELECT C.Field1, M.Field2
FROM Contractors C INNER JOIN Meters M ON C.ID = M.ID
WHERE M.FeedStatus <> 'A'

But this does :-

SELECT C.Field1, M.Field2
FROM Contractors C INNER JOIN Meters M ON C.ID = M.ID
WHERE (M.FeedStatus <> 'A' OR M.FeedStatus IS NULL)

My queation is, why does the 1st SQL not return NULL values, and do you always have to check for NULL when using "<>".

Sorry if i'm being a bit thick, and thanks in anticipation of your advice.

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-02-28 : 06:03:33
"why does the 1st SQL not return NULL values"

Because NULL can not be compared with anything using normal relational operators. NULL <> 'A' will always yield NULL, not false.
So the query wont be successful in retrieving records for NULL status.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-02-28 : 06:05:10
See on from Books OnLine
quote:
<> (Not Equal To)
Compares two expressions (a comparison operator). When you compare nonnull expressions, the result is TRUE if the left operand is not equal to the right operand; otherwise, the result is FALSE. If either or both operands are NULL and SET ANSI_NULLS is set to ON, the result is NULL. If SET ANSI_NULLS is set to OFF, the result is FALSE if one of the operands is NULL, and TRUE if both operands are NULL.



KH

Go to Top of Page

itsmarkdavies
Starting Member

14 Posts

Posted - 2007-02-28 : 06:08:22
Thanks to both "harsh_athalye" and "khtan" for your replies - i learn something every day !.
Go to Top of Page
   

- Advertisement -