views:

60

answers:

2

So I have a query. It works if I comment out the part checking for the exclusion.

WHERE H.BatchId = 3  AND H.IsExcluded != 1

IsExcluded is a bit field that accepts NULL values.

All the values I'm selecting from are NULL, so that SHOULD be right. What am I doing wrong? It's returning no values the way it is, but if I comment out just the 'AND' and after, it works.

+9  A: 
WHERE H.BatchId = 3  AND (H.IsExcluded != 1 OR H.IsExcluded IS NULL)
Flakron Bytyqi
wow I was doing H.IsExcluded = NULL. THANK YOU! Accepted once it lets me.
Scott
+1 - @Scott - NULL has no value (SQL thinks it could have ANY value or no value), so it won't respond normally to operators.
JNK
Good to know. I'm still learning. Been working professional (after college) for a few months now. Picking up stuff as I go along.
Scott
@Scott: Now (after college) is when you will *really* learn stuff. ;)
FrustratedWithFormsDesigner
I seem to agree with that so far, haha.
Scott
A: 

in SQL NULL != NULL as @scott said but if can be if you SET ANSI NULL OFF

Declare @a INT = NULL
Declare @b INT = NULL
SET ANSI_NULLS OFF
IF(@b = @a)
    PRINT 'NULL is eaqul to NULL'
ELSE
        PRINT 'NULL is not eaqul to  NULL'

SET ANSI_NULLS ON
IF(@b = @a)
    PRINT 'NULL is eaqul to NULL'
ELSE
        PRINT 'NULL is not eaqul to  NULL'

Output is NULL is eaqul to NULL --> Here SET ANSI_NULLS OFF is done NULL is not eaqul to NULL--> Here SET ANSI_NULLS ON is done. By default SQL Server has this setting

Ashwani Roy