Oracle SQL_Null Related functions-1_NVL and NVL2

apple-apple-devices-clean-205316

admin Functions, Oracle

Hello guys,
In this blog, we will talk about Null related functions. In Oracle documentation these functions are categorised under Single-Row Functions.

What are the Null related functions?

Null related functions are those functions which let user handle Null values in the query output.
It means if these are some null values in any column, we can replace null values with anything we want.
In order to learn the use of null related functions, we will start with NVL and then NVL2.

NVL Function
Syntax of NVL function is pretty simple.

Syntax:
NVL(expr1, expr2)

In the above syntax, expr1 and expr2 are columns from the table. In some cases, expr2 can be a char value.
If expr1 is null then NVL returns expr2 and if expr1 is not null then NVL returns expr1.
expr1 and expr2 can have any data type. Oracle engine can convert data type of expr2 implicitly to expr1 data type or sometimes it produces an error.
The implicit conversion is implemented as follows:
• If expr1 is character data, then Oracle Database converts expr2 to the data type of expr1 before comparing them and returns VARCHAR2 in the character set of expr1.
• If expr1 is numeric, then Oracle Database raise an error.

Let’s connect to HR schema of our Oracle database.

In the above sql, commission_pct and salary have the same data type in the table. So where commission_pct column does not have any value or have Null value, NVL function is retuning values from the Salary column of the table.

Let’s try this function with different data types.

Above SQL query will return an error because data type of expr2 is not matching with expr1 and Oracle engine is not able to convert data type implicitly.
To overcome this error, we need to add an addition conversion function to change the data type of commission_pct column to the data type of expr2.

Note:
TO_CHAR() function is used to convert any data type to VARCHAR2 data type. I will publish a detailed blog separately on the conversion functions and introduce this function there.

Home work:
Create a table with some data where a column with VARCHAR2 data type has some NULL values and then use NVL function to display numeric values in that column.

NVL2 Function
Syntax of NVL2 function is little different than NVL function.
Syntax:
NVL2(expr1, expr2, expr3)

In the above syntax, expr1 must be a column name and expr2 and expr3 can be columns from the table or any values of any data type.
If expr1 is not null then NVL2 function will return expr2 and if expr1 is null then it will return expr3.
Data type conversion rules are same as NVL function.

Let’s do an example and see how it works.

You can see in the output that where commission_pct (expr1) is null, Oracle database is showing Salary (expr3) as Income and where commission_pct (expr1) is not null, Oracle database is calculating expr2 and showing as Income.

That’s all for NVL and NVL2 function. Try more examples with different data types to have more understanding of NVL and NVL2 fuctions.
If you have any questions please comment below or write me an email.

If you like this blog, do share with your friends and colleagues on social media.
For more updates join my facebook group.

Thank you,
Kapil

Sharing is caring!

You May Also Like..

merge

SQL – MERGE Statement

Hi Devs, As we have already learned INSERT INTO and UPDATE statement, our next very important statement is MERGE. Merge […]

update2

SQL – UPDATE Statement part-2

Hi Geeks, In previous blog on UPDATE statement, we learnt, how can we update a column value with a string […]

update

SQL – UPDATE Statement part-1

Hi Geeks, We already have a separate blog on INSERT INTO statement. So today, we will talk about UPDATE statement […]

Leave a Reply

Your email address will not be published. Required fields are marked *