Oracle Database

What is the Difference Between Oracle NVL and Coalesce?

While working with Oracle databases, users often face the null values (missing or unknown data) stored in the tables. If the null values are used in any calculations or comparisons, an error can occur. To avoid this issue, Oracle offers Oracle NVL and Coalesce. They are two built-in functions to handle null values in queries and other operations.

This blog will cover the enlisted content:

Overview of Oracle NVL

The Oracle NVL function is a built-in Oracle database function that aids users to replace null values with a specified default value to avoid any error in calculations or reports. It takes two arguments as input and checks the first argument to see whether it is null or not, in the case of the null value it replaces it with the second value. However, if the data type is different for both arguments it implicitly converts the data type of the second argument.

Syntax

NVL(expr, replacement_value)

In the above syntax, the ā€œNVLā€ function can take only two arguments. The ā€œexprā€ (stands for expression) should be checked if it is null or not. In the case of null, it should be replaced by a second argument (replacement_value).

Practical Implementation

For the practical implementation, letā€™s first show the values of a table named ā€œCOUNTRIESā€ using this command:

SELECT * FROM COUNTRIES;

The above command lists all values stored in the ā€œCOUNTRIESā€ table.

Output

The output depicts the values of the table, users can see null values stored in the column name ā€œREGION_IDā€.

It’s time to run the provided command to replace the null values of column ā€œREGION_IDā€ in the table ā€œCOUNTRIESā€ with the value ā€œ0ā€:

SELECT NVL(REGION_ID, 0) FROM COUNTRIES;

The above command passed two arguments ā€œREGION_IDā€ which will be checked for null values and ā€œ0ā€ which will be replaced with null values.

Output

The output returned a column with the value ā€œ0ā€ in place of null values.

Overview of Oracle Coalesce

The Oracle Coalesce function (use the ā€œshort-circuit evaluationā€ feature) is a built-in Oracle database function that aids the user to replace the null values with the first not null value from the specified multiple arguments. However, if all the input arguments will be null, the user will get the null output.

Syntax

COALESCE(expr, replacement_value1, replacement_value2, replacement_value3 ...)

In the above syntax, the ā€œCoalesceā€ can take multiple arguments. The ā€œexprā€ argument checks for being null or not. In case it is null, it will be replaced with the first not null ā€œreplacement_valueā€.

Practical Implementation

Letā€™s use a ā€œDESTINATIONā€ table for the practical implementation of the ā€œCoalesceā€ function. But before that, execute the below command to enlist all the stored values of this table:

SELECT * FROM DESTINATION;

The above command depicts the stored values of the ā€œDESTINATIONā€ table.

Output

The output displayed the table values and here users can see the column ā€œSTATEā€ with some null values.

Execute the given command to utilize the ā€œCOALESCEā€ function to replace the null values:

SELECT COALESCE(STATE,NULL,NULL,'U','K') FROM DESTINATION;

The above command comprises multiple arguments which replace the null values of the ā€œSTATEā€ column with the first non-null argument.

Output

The output returns a column where the null values of the ā€œSTATEā€ column have been replaced by the first not null value which is ā€œUā€.

Difference Between Oracle NVL and Coalesce

Let’s discuss the differences between the Oracle NVL and Oracle Coalesce functions:

Parameters Oracle NVL Oracle Coalesce
Number of Arguments It accepts only two arguments as input for the function. It can take two or more arguments as input for the function.
Return Type It always returns the same data type as the first argument. If the data type is changed it implicitly changes the data type of the second argument. It returns the data type of the first non-null argument encountered.
Evaluation of Arguments It checks the first argument and in case it is null the second argument is returned It checks each argument starting from the second position until a non-null value is found and replaces it with the null value of the first argument.
Performance If the requirement is to check only two arguments the NVL is faster than coalesce. If the requirement is to handle multiple arguments, then coalesce is faster than NVL as it only evaluates each argument until a non-null value is found

Conclusion

Oracle NVL and Oracle Coalesce are useful Oracle database functions for handling null values to prevent calculation errors and make reports more meaningful. NVL is a simpler and faster function for handling two arguments while coalesce is a more versatile and efficient function when the user wants to handle multiple arguments. This post has discussed Oracle NVL, Oracle Coalesce, and their differences.

About the author

Nimrah Ch

Iā€™m a technical writer with a Bachelor's in Software Engineering and Certification in Cyber Security. Through my research and writing, I strive to provide readers with in-depth, informative articles that can help them make informed decisions about their technological needs.