33

How do I select rows which don't equal a value and also include nulls in the returned data? I've tried:

SET ANSI_NULLS OFF
SELECT TOP 30 FROM Mails
WHERE assignedByTeam <> 'team01'

I want to return rows which don't have 'team01' in column assignedByTeam but I also want results containing nulls. Unfortunately, the above code doesn't work (doesn't return the nulls).

I'm using MS SQL Server 2008 Express.

5 Answers 5

36

Try checking for NULL explicitly:

SELECT TOP 30 col1, col2, ..., coln
FROM Mails
WHERE (assignedByTeam <> 'team01' OR assignedByTeam IS NULL)
2
  • 1
    Well, yeah but I thought there was a shorter way of doing this, like just one command instead of writing a few of them. Anyways, thanks for the effort!
    – Val
    Commented May 2, 2012 at 20:46
  • 6
    @ValCool: In the SQL standard there is an operator for this called IS DISTINCT FROM but SQL Server doesn't support it. MySQL has the non standard null-safe equality operator NOT a <=> b, but SQL Server can't do this either.
    – Mark Byers
    Commented May 2, 2012 at 21:00
21
SELECT TOP 30 FROM Mails
WHERE ISNULL(AssignedByTeam,'') <> 'team01'

I saw a coalesce statement version but ISNULL() is more efficient.

7
  • 2
    This is the most elegant solution in my opinion.
    – Amarundo
    Commented Aug 22, 2016 at 20:43
  • 1
    What's the execution plan look like for that? Will it still use the index? Commented Oct 8, 2018 at 22:46
  • 1
    The index will not be used. For performance, WHERE (assignedByTeam <> 'team01' OR assignedByTeam IS NULL) is much much better Commented Jul 15, 2020 at 16:12
  • @ReversedEngineer Is index work for not equal operator(<>)? You have to find all values except team01. So this is already not searchable by index.
    – doctorgu
    Commented Mar 16, 2022 at 0:14
  • @doctorgu Why should the index not be used for the <> operator? I remember reading that the query optimiser views it as WHERE (assignedByTeam < 'team01' OR assignedByTeam > 'team01' OR assignedByTeam IS NULL), where the index can be used to great advantage. Commented Mar 16, 2022 at 11:13
12

When you have a lot of conditions, typing everything twice stinks. Here are two better alternatives:

SELECT TOP 30 FROM Mails
WHERE COALESCE(assignedByTeam,'') <> 'team01'

The COALESCE operator returns the first non-null value in the list. If assignedByTeam is NOT null, it will compare the assignedByTeam value to 'team01'. But if assignedByTeam IS null, it will compare a blank '' to 'team01'. It's basically shorthand for the following:

SELECT TOP 30 FROM Mails
WHERE (CASE WHEN assignedByTeam IS NULL THEN '' ELSE assignedByTeam END) <> 'team01'

The second way is to make your condition conditional, for example:

SELECT TOP 30 FROM Mails
WHERE 1 = CASE WHEN assignedByTeam = 'team01' THEN 0 ELSE 1 END

In this example, the ELSE value will include all null rows, since they aren't equal to 'team01'.

0
4
SELECT TOP 30 FROM Mails
WHERE assignedByTeam <> 'team01'
OR assignedByTeam is null
2
 where column != 'value' or column is null
0

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.