Also include if field2
is NULL
. You know the null isn't a match but want to show it.
SELECT * FROM table
WHERE field1 != field2
OR (field1 IS NULL AND field2 IS NOT NULL)
OR (field1 IS NOT NULL AND field2 IS NULL)
Additionally, you can use COALESCE
to assert the null
as another value. I used 0 in this case. Only use 0 if there is no 0 for field1
or field2
. Basically choose a value that won't happen in your table.
select * from table where COALESCE(field1, 0) != COALESCE(field2, 0)
Edit: OP asserted that field1 could be null, so I changed the first query. Query 1 is a bit more clear on what's going on, and Query 2 is a more concise way to achieve the same end result.
Read more here: COALESCE Function in TSQL
You can also use NVL()
to deal with NULL
like COALESCE
, however it is less efficient to my understanding. More info: Oracle Differences between NVL and Coalesce