0

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.

2 Answers 2

9
WHERE H.BatchId = 3  AND (H.IsExcluded != 1 OR H.IsExcluded IS NULL)
5
  • 1
    wow I was doing H.IsExcluded = NULL. THANK YOU! Accepted once it lets me.
    – Scott
    Aug 3, 2010 at 14:31
  • 2
    +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
    Aug 3, 2010 at 14:32
  • 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
    Aug 3, 2010 at 14:34
  • 1
    @Scott: Now (after college) is when you will really learn stuff. ;) Aug 3, 2010 at 14:36
  • I seem to agree with that so far, haha.
    – Scott
    Aug 3, 2010 at 14:38
0

In SQL NULL != NULL as @scott said but you may use SET ANSI_NULLS OFF.

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

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

Output is:

  • NULL is equal to NULL: ANSI_NULLS set to OFF
  • NULL is not equal to NULL: ANSI_NULLS set to ON, this is the default SQL Server behaviour.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.