Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T-SQL: select rows not equal to a value, including nulls

Tags:

tsql

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.

like image 848
Val Avatar asked May 02 '12 20:05

Val


People also ask

Does not equal include NULL values SQL?

In SQL null is not equal ( = ) to anything—not even to another null . According to the three-valued logic of SQL, the result of null = null is not true but unknown. SQL has the is [not] null predicate to test if a particular value is null .

Is is not NULL and != The same in SQL?

<> is Standard SQL-92; != is its equivalent. Both evaluate for values, which NULL is not -- NULL is a placeholder to say there is the absence of a value.

Can you use != In SQL?

We can use both SQL Not Equal operators <> and != to do inequality test between two expressions. Both operators give the same output.


1 Answers

Try checking for NULL explicitly:

SELECT TOP 30 col1, col2, ..., coln FROM Mails WHERE (assignedByTeam <> 'team01' OR assignedByTeam IS NULL) 
like image 52
Mark Byers Avatar answered Sep 30 '22 23:09

Mark Byers