Quantcast
Channel: dBforums – Everything on Databases, Design, Developers and Administrators
Viewing all articles
Browse latest Browse all 13329

Why does a WHERE clause on a nullable field not return the null records?

$
0
0
I recently ran into an issue with an issue with a query against our Data Warehouse. When attempting to sum revenue from a table, and using a WHERE clause on a field that contains NULL values, the records with the NULL values are suppressed (in addition to whatever the WHERE clause specified). I believe this is because a NULL value is unknown so SQL doesn't know if it does or doesn't fit the criteria of there WHERE clause so it is suppressed (correct me if i am wrong).

That being said, is there a way to avoid this instead of having to add an ISNULL function in the WHERE clause which is going to kill performance?

Code:

create table #nullTest (
name varchar(50)
,revenue int)

INSERT INTO #nullTest
Values ('Tim',100)
,('Andrew', 50)
,(null, 200)

SELECT sum(revenue) as Revenue FROM #nulltest WHERE name <> 'tim'

Ideally, I would want the SELECT statement above to return 250, not 50. The only way I can think to accomplish this is with this query:
Code:

SELECT sum(revenue) as Revenue FROM #nullTest WHERE isnull(name,'') <> 'tim'

Viewing all articles
Browse latest Browse all 13329

Trending Articles