SQL Tutorial
SQL Clauses / Operators
SQL-Injection
SQL Functions
SQL Queries
PL/SQL
MySQL
SQL Server
Misc
In SQL, especially in Oracle, there are several functions provided to handle nulls and make conditional evaluations. Here are some commonly used general functions:
The NVL
function is used to replace null values with a default value.
Syntax:
NVL(expression, default_value)
If the expression
is null, NVL
returns the default_value
.
The NVL2
function is similar to NVL
, but it works a bit differently. It returns the second value if the first one is NOT null, and the third value if the first one is null.
Syntax:
NVL2(expression, value_if_NOT_null, value_if_null)
The DECODE
function provides if-then-else logic within SQL.
Syntax:
DECODE(expression, search_value1, result1 [, search_value2, result2,...,] [, default_result])
It compares expression
to each search_value
. If expression
is equal to a search_value
, it returns the corresponding result
.
The COALESCE
function returns the first non-null value in its argument list.
Syntax:
COALESCE(value1, value2, ..., valueN)
The NULLIF
function compares two expressions. If they are equal, it returns null; otherwise, it returns the first expression.
Syntax:
NULLIF(expression1, expression2)
The LNNVL
function is used to evaluate a condition and return FALSE
if the condition is TRUE
, TRUE
if the condition is FALSE
, and TRUE
if the condition is unknown.
Syntax:
LNNVL(condition)
The NANVL
function is specifically for floating-point numbers. It returns an alternative value if the input is NaN (Not a Number).
Syntax:
NANVL(expression, alternative_value)
If the expression
is NaN, NANVL
returns the alternative_value
.
Note: While many of these functions, like COALESCE
and NULLIF
, are standard across various RDBMS, others like NVL
, NVL2
, and DECODE
are specific to Oracle. Always refer to the documentation of the RDBMS you are working with to understand the availability and nuances of each function.
SELECT employee_name, NVL(salary, 0) AS adjusted_salary FROM employees;
SELECT employee_name, NVL2(commission, 'Eligible for Commission', 'Not Eligible') AS commission_status FROM employees;
SELECT COALESCE(column1, column2, column3, 'No Value') AS result FROM table1;
SELECT NVL(column1, 'No Value') AS result FROM table1;
SELECT NULLIF(column1, 0) AS result FROM table1;
SELECT employee_name FROM employees WHERE LNNVL(salary > 50000);
SELECT column1, NANVL(column2, 0) AS adjusted_value FROM table1;
SELECT employee_name, CASE WHEN salary > 50000 THEN 'High Salary' ELSE 'Low Salary' END AS salary_category FROM employees;
SELECT employee_name, DECODE(salary > 50000, 1, 'High Salary', 'Low Salary') AS salary_category FROM employees;
SELECT COALESCE(column1, column2, column3, 'No Value') AS result FROM table1;
SELECT CASE WHEN column1 IS NOT NULL THEN column1 WHEN column2 IS NOT NULL THEN column2 ELSE 'No Value' END AS result FROM table1;
SELECT employee_name, NVL(NULLIF(salary, 0), 50000) AS adjusted_salary FROM employees;