Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing nulls in Oracle SQL

Tags:

sql

null

oracle

Is there a nicer way of comparing if two values are equal if they can be nulls other than the following?

a = b or (a is null and b is null)
like image 508
Clinton Avatar asked Jun 27 '11 03:06

Clinton


People also ask

How do I compare NULL values in SQL?

How to Test for NULL Values? It is not possible to test for NULL values with comparison operators, such as =, <, or <>. We will have to use the IS NULL and IS NOT NULL operators instead.

Can we compare two NULL values in SQL?

To handle NULLs correctly, SQL provides two special comparison operators: IS NULL and IS NOT NULL. They return only true or false and are the best practice for incorporating NULL values into your queries. Now the query will return every row, as we expected.

How do you compare NULLs?

Say you have a table with a NULLable column type, and you want to find rows with a NULL value in that column. Since you can't use a equality operator in the WHERE clause (remember, NULL values can't be equated or compared), the right way to compare NULL values is to use the IS and IS NOT operators.

Can we compare NULL with NULL in 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 .


1 Answers

You can:

a=b or coalesce(a,b) is null

You could also use nvl, but that is a legacy function and coalesce is quicker, since it stops at the first non-null

like image 118
vol7ron Avatar answered Sep 19 '22 04:09

vol7ron