Skip to main content
Log inGet a demo
Logical operators

SQL NANVL

What is SQL NANVL?

The SQL NANVL (Not-a-Number Value Logic) function is used to evaluate expressions and return a specified value when the expression evaluates to a NaN (Not-a-Number) value. NaN is a special value typically used to represent undefined or unrepresentable values in numeric calculations. NANVL is primarily used in Oracle Database and other systems that support the IEEE 754 floating-point standard.

When you would use it

You would use the SQL NANVL function when you want to handle NaN values in numeric calculations and replace them with a specific value to ensure that calculations or results are meaningful. It is commonly used in scientific or financial applications where numeric computations may result in NaN values.

Syntax

The syntax for the SQL NANVL function is as follows:

NANVL(expression, replacement_value)
  • expression: The expression you want to evaluate. If it evaluates to NaN, it is replaced with the replacement_value.
  • replacement_value: The value to use as a replacement when expression is NaN.

Parameter values

  • expression: The expression you want to evaluate for NaN values.
  • replacement_value: The value to use as a replacement when expression is NaN.

Example query

Suppose we have a table named "transactions" with columns "transaction_id" and "transaction_amount." We want to calculate the average transaction amount, replacing NaN values with 0 using the NANVL function:

SELECT NANVL(AVG(transaction_amount), 0) AS average_amount
FROM transactions;

In the above query, we use the NANVL function to calculate the average transaction amount and replace any resulting NaN value with 0.

Example table response

Assuming the "transactions" table contains the following data:

| transaction_id | transaction_amount |
|--------------- | ------------------ |
| 1             | 100.00             |
| 2             | 50.00              |
| 3             | NULL               |
| 4             | 75.00              |
| 5             | 120.00             |

The query mentioned earlier would return the following result:

| average_amount |
|--------------- |
| 71.25         |

This result calculates the average transaction amount, and since there is a NULL value in the "transaction_amount" column, it uses the NANVL function to replace the resulting NaN with 0.

Use cases

  • Handling NaN values in numeric calculations, ensuring meaningful results.
  • Avoiding disruptions in calculations or aggregations due to NaN values.
  • Providing a default value when numeric expressions result in NaN.

SQL languages this is available for

The SQL NANVL function is primarily available in Oracle Database. It is specific to databases that support the IEEE 754 floating-point standard and NaN values. Other database systems may use different functions or approaches to handle NaN values in numeric calculations. If you are using a specific database system, consult its documentation for the equivalent function or approach for handling NaN values.


Keep reading

SQL LNNVL

Read article

SQL INTERVAL

Read article

SQL % Wildcard

Read article

Ready to put your SQL knowledge to work?

Practice writing SQL to call data from the warehouse and sync it into Google Sheets in this 5 minute interactive demo.

Hightouch Audiences user interface.