Query not returning values for NULL

ssn

I am having a bit of trouble with a query for SQL Server 2008.

I have a table with some values and a category. This category can be e.g. Stock, Bond or NULL.

Then I may want to see everything in my table that is not bonds:

SELECT Value, Name, Currency, Assetclass
FROM MyTable
WHERE Assetclass <> 'Bond'

Here I expect to see all my assets that are Stock and uncategorised (NULL). But instead I only see the stocks. I get the same result as setting my Where-condition to Assetclass = 'Stock'.

I am aware that NULL is treated as an unidentified value, but I would expect it to only disregard rows that contain exactly 'Bond' and keep everything else, but this is apparently not the case?

David Hedlund

As others have pointed out, this is the expected behavior. If you don't want to do an OR you could always replace null with something else in your comparison:

WHERE ISNULL(Assetclass, 'Anything but Bond') <> 'Bond'

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related