0

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?

4
  • WHERE (Assetclass <> 'Bond' or Assetclass Is null) ?? Commented Oct 3, 2014 at 8:46
  • @huMptyduMpty -> This will not work - I am interested in selecting everything except the few rows where the assetclass actually contains 'Bond' Commented Oct 3, 2014 at 8:47
  • @Steffen: It will work. It will select all non-null values except bond (satisfies the Assetclass <> 'Bond' condition) as well as all null values (satisfies the Assetclass is null condition). Commented Oct 3, 2014 at 8:52
  • @DavidHedlund I agree it should, but I received a lot of unexpected rows using this and can't really explain why. EDIT: Ah forgot the parenthesis ;-) Commented Oct 3, 2014 at 8:57

4 Answers 4

3

This is the expected behaviour.

You are asking for all the rows that have a value that is different from 'Bond'.

NULL is not a value but a 'marker' stating that the system have no clue about the content of that field; being the content unknown the system cannot say for sure that the value is different from 'Bond' hence the row is not returned.

Sign up to request clarification or add additional context in comments.

Comments

2

You can't compare NULL values using <>, you should change your SQL to:

SELECT Value, Name, Currency, Assetclass
FROM MyTable
WHERE Assetclass <> 'Bond' OR Assetclass IS NULL;

In MySQL you have null safe comparators but that is not the case in SQL Server.

The idea is that NULL is not a value, therefore it can't be compared to other values. However, you can check if a field is NULL using IS NULL.

Please, check this question for more insight: Why is null<>null=null in mysql

Comments

1

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'

Comments

1

Projection will ignore null values. Use Isnull function

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


OR 


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

I would prefer the first approach

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.