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 2005 Forums
 Transact-SQL (2005)
 What's going on with this very simple query?

Author  Topic 

jackallan
Starting Member

2 Posts

Posted - 2009-06-09 : 13:27:30
OK I'm perplexed by this...

I just ran this query:
SELECT COUNT(*) FROM Cases WHERE case_installtype IS NULL
SELECT COUNT(*) FROM Cases WHERE NOT (case_installtype = 'CRMInstalled' OR case_installtype = '1')

And got this result:

-----------
29018
(1 row(s) affected)

-----------
28
(1 row(s) affected)


How is this possible!??!?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-06-09 : 13:36:07
second case it ignores the null valued records altogether. that why you get only 28 rows.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-06-09 : 13:38:02
null is not stored as a value internally. it just represents lack of a defined value
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-06-09 : 13:44:31
you need to explicitly check for NULL in the second statement like
SELECT COUNT(*) FROM Cases WHERE case_installtype NOT IN ('CRMInstalled' ,'1') OR case_installtype IS NULL

quote:
Originally posted by jackallan

OK I'm perplexed by this...

I just ran this query:
SELECT COUNT(*) FROM Cases WHERE case_installtype IS NULL
SELECT COUNT(*) FROM Cases WHERE NOT (case_installtype = 'CRMInstalled' OR case_installtype = '1')

And got this result:

-----------
29018
(1 row(s) affected)

-----------
28
(1 row(s) affected)


How is this possible!??!?

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-06-09 : 13:54:35
quote:
Originally posted by vijayisonly

you need to explicitly check for NULL in the second statement like
SELECT COUNT(*) FROM Cases WHERE case_installtype NOT IN ('CRMInstalled' ,'1') OR case_installtype IS NULL

quote:
Originally posted by jackallan

OK I'm perplexed by this...

I just ran this query:
SELECT COUNT(*) FROM Cases WHERE case_installtype IS NULL
SELECT COUNT(*) FROM Cases WHERE NOT (case_installtype = 'CRMInstalled' OR case_installtype = '1')

And got this result:

-----------
29018
(1 row(s) affected)

-----------
28
(1 row(s) affected)


How is this possible!??!?




unless you've set ANSI NULLs setting to OFF
Go to Top of Page

jackallan
Starting Member

2 Posts

Posted - 2009-06-10 : 04:28:48
Thanks guys!
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-06-10 : 04:33:49
See http://weblogs.sqlteam.com/markc/archive/2009/06/08/60929.aspx


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page
   

- Advertisement -