0

I have an oracle DB query where I want to fetch all such records from a table where the value of DECISION is not equal to DISCARD. I ran the below query:

select * from TABLE where DECISION != 'Discard'

I had two records in my database but for both the DECISION was null ,which is not equal to DISCARD. However, I did not get any record.

So, I had to change the query to below which fetched the records successfully.

select * from TABLE where (DECISION != 'Discard' or DECISION is null)

I am confused about what is the difference between the two.

2 Answers 2

2

You may use NVL

select *  from TABLE where NVL(DECISION,'x') != 'Discard'

The "problem" is in the three value SQL logik

if DECISION is NULL than DECISION != 'Discard' returns not TRUE but UNKNOWN (it means it is not known if DECISION is Discard or not).

See details in Oracle Documentation

a != NULL gives UNKNOWN for any value of a

To get all rows testing a nullable column you must use NVL or possible COALESCE (= more "standard")

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

2 Comments

Thanks for the explanation.
@Ashish Unfortunately not as simple. SQL implements a three value logik with TRUE, FALSE and UNKNOWN, so you can't imply not true --> false. See overview in the docs
2

By design / definition, NULL is a special value indicating unknown/inapplicable. Any comparison made to NULL is UNKNOWN, which in Oracle's three-valued logic (https://en.wikipedia.org/wiki/Three-valued_logic) is neither TRUE nor FALSE.

Your 1st predicate:

where DECISION != 'Discard'

evaluates to UNKNOWN for records having a NULL decision. The WHERE clause only includes rows where the predicate is TRUE. Since UNKNOWN is not the same as TRUE, your 1st predicate excludes NULL values.

Your 2nd predicate:

where (DECISION != 'Discard' or DECISION is null)

Evaluates to (UNKNOWN OR TRUE), which in turn evaluates to TRUE. So, your 2nd predicate includes the NULL values.

1 Comment

This is not correct as documented here. 1 = null is UNKNOWN and not false. If it would be false the negation of it (NOT) would be true, which it is not! Please adjust to not spread confusion in an already confusing logik;)

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.