I am enrolled at the Art Institute of Pittsburgh - Online working towards a Bachelor of Science in photography.
My other blog [blogspot.com]
Today is another one of those days.
I'm doing a query against a table using a primary key and a check if two of the other columns are not the same.
SELECT column_list FROM table_name WHERE pk_column = some_var AND column_x != column_y;
One of the columns can be null. Apparently I have never tried to do this with Oracle until now.
Initially I expected that NULL is not equal to some non-NULL value in the same way that an undef is not equal some defined value. However, I know why Oracle does this (it's in the ANSI standard but no one else that I know of does it this way).
Don't Shoot Me... (Score:2)
... I didn't design it.
The new query looks like:
SELECT column_list FROM table_name WHERE pk_column = some_var AND ( column_y IS NULL OR column_x != column_y);
PostgreSQL too (Score:2)
Re: (Score:2)
Logically incomparable? (Score:1)
You have something, and you have nothing. And you can't compare something with nothing. Thus, they can't be "different" because there's nothing there to be different with.
Takes a bit of mind reversal...
NVL() (Score:1)
Re: (Score:1)
Re: (Score:2)
Re: (Score:2)
And if you want to be SQL-92 compliant, you can use
COALESCE, which works the same asNVL(but sounds better, even though it takes longer to type).Re: (Score:1)
What does COALESCE do? (As opposed to NVL).
Re: (Score:1)
Is that right?
I am thinking right now that the correct way to write this test would be
WHERE NVL(col1 != col2, FALSE). So it would return the resulting boolean value from all comparisons of existent values, but for comparisons involvingNULLit would default the resultingNULLtoFALSE. Basically you’re saying “compare the columns for inequality, and if they can’t be compared, then treat them as unequal.”By doing it this way you avoid semi-predicate problems.
Re: (Score:1)
MySQL too (Score:1)
Re: (Score:2)
Re: (Score:1)
Maybe I'm Just Braindead... (Score:2)
... but I really thought that I had seen a database treat NULLs like Perl treats undefs. I guess it's always possible that my memory is playing tricks on me. It certainly wouldn't be the first time. I did a lot of stupid stuff in my youth that almost certainly destroyed vast amounts of brain cells.
The absence of a value shouldn't prevent you from checking [in]equality. But maybe I'm the only one that thinks that way. :)