What is the usage of NVL() function?

The NVL() function is used to convert NULL value to the other value. NVL() function is used in Oracle it is not in SQL and MySQL server.

Instead of NVL() function MySQL have IFNULL() and SQL Server have ISNULL() function.

In SQL, the NVL() function is typically used to replace NULL values with a specified default value. It is commonly used in scenarios where you want to handle NULL values by substituting them with a known or default value.

The syntax for the NVL() function may vary slightly depending on the database system you are using. Here is a generic example:

NVL(column_name, default_value)

In this syntax:

  • column_name is the column or expression that may contain NULL values.
  • default_value is the value that will be returned if column_name is NULL.

For example, if you have a column named salary in a table, and you want to display ‘Not available’ for employees with NULL salary values, you can use the NVL() function like this:

SELECT NVL(salary, 'Not available') AS actual_salary
FROM employees;

In the above query, if the salary is NULL, it will be replaced with the default value ‘Not available’ in the result set.