Oracle Tutorial

  • Home
  • Start Here
  • Basics
  • Advanced
    • Oracle View
    • Oracle Index
    • Oracle Synonym
    • Oracle Sequence
    • Oracle Administration
  • PL/SQL
  • Functions
    • Aggregate Functions
    • Analytic Functions
    • Comparison Functions
    • Date Functions
    • String Functions
  • API
    • Python Oracle
Home / Oracle Comparison Functions / Oracle NULLIF Function

Oracle NULLIF Function

Summary: in this tutorial, you will learn how to use the Oracle NULLIF() function by practical examples.

Introduction to Oracle NULLIF() function

The Oracle NULLIF() function accepts two arguments. It returns a null value if the two arguments are equal. In case the arguments are not equal, the NULLIF() function returns the first argument.

The following illustrates the syntax of the Oracle NULLIF() function:

NULLIF(e1, e2);

In this syntax, the e1 cannot be a literal NULL.

If both expressions evaluates to numeric values, then Oracle determines the argument with the higher numeric precedence, implicitly converts the other argument to that data type, and returns a value of that data type.

In case both expressions evaluate to non-numeric values, then they must be of the same data type, otherwise, Oracle issues an error.

For example, the following statement returns a null value because the first argument equals the second one.

SELECT NULLIF(100,100) -- null FROM dual;

However, the following example returns the first value (100) because the two arguments are different:

SELECT NULLIF(100,200) -- 0 FROM dual;

The following example causes an error because the first argument is literal NULL:

SELECT NULLIF(NULL,100) FROM dual;

Here is the error:

ORA-00932: inconsistent datatypes: expected - got CHAR

The following statement also causes an error because the data types of arguments are different.

SELECT NULLIF(10,'20') FROM dual;

The error is:

ORA-00932: inconsistent datatypes: expected NUMBER got CHAR

To fix it, we use the TO_CHAR() function to convert the first argument to a value of character data type:

SELECT NULLIF(TO_CHAR(10),'20') -- 10 FROM dual;

The NULLIF() function is logically equivalent to the following searched CASE expression:

CASE WHEN e1 = e2 THEN NULL ELSE e1 END

Oracle NULLIF() function example

The following statement creates a table named budgets that stores sales employees and their current and previous year sales budgets.

CREATE TABLE budgets ( salesman_id NUMBER NOT NULL, fiscal_year SMALLINT, current_year NUMBER, previous_year NUMBER );

If a budget has not been determined, it is null. The following statements insert budgets for some sales employees:

INSERT INTO budgets VALUES(54,2017,120000, 100000); INSERT INTO budgets VALUES(55,2017,200000, 200000); INSERT INTO budgets VALUES(56,2017,NULL, 150000); INSERT INTO budgets VALUES(57,2017,175000, 175000); INSERT INTO budgets VALUES(59,2017,220000, 200000);

The following query retrieves the sales employees and their current year’s budgets. If the current year’s budget is the same as the previous year’s one, it returns a literal string “Same as last year”:

SELECT salesman_id, COALESCE(TO_CHAR(NULLIF(current_year, previous_year)), 'Same as last year') budget FROM budgets WHERE current_year IS NOT NULL;
Oracle NULLIF example

The following statement is equivalent to the one above but uses the CASE expression instead.

SELECT salesman_id, CASE WHEN current_year = previous_year THEN 'Same as last year' ELSE TO_CHAR(current_year) END FROM budgets WHERE current_year IS NOT NULL;

In this tutorial, you have learned how to use the Oracle NULLIF() function to return a null if the first argument equals the second one.

  • Was this tutorial helpful?
  • YesNo
Previous Oracle COALESCE Function
Next Oracle NVL Function

Getting Started

  • What Is Oracle Database
  • Install Oracle Database Server
  • Download Oracle Sample Database
  • Create Oracle Sample Database
  • Connect To Oracle Database Server

Oracle Data Manipulation

  • SELECT
  • Oracle DUAL Table
  • ORDER BY
  • SELECT DISTINCT
  • WHERE
  • Table & Column Aliases
  • AND
  • OR
  • FETCH
  • BETWEEN
  • IN
  • LIKE
  • IS NULL
  • Joins
  • INNER JOIN
  • LEFT JOIN
  • RIGHT JOIN
  • FULL OUTER JOIN
  • CROSS JOIN
  • Self Join
  • GROUP BY
  • HAVING
  • UNION
  • INTERSECT
  • MINUS
  • GROUPING SETS
  • CUBE
  • ROLLUP
  • PIVOT
  • UNPIVOT
  • INSERT
  • INSERT INTO SELECT
  • INSERT ALL
  • UPDATE
  • DELETE
  • MERGE
  • Subquery
  • Correlated Subquery
  • EXISTS
  • NOT EXISTS
  • ANY
  • ALL

Oracle Data Types

  • Oracle Data Types
  • NUMBER
  • FLOAT
  • BINARY_FLOAT
  • CHAR
  • NCHAR
  • VARCHAR2
  • NVARCHAR2
  • DATE
  • INTERVAL
  • TIMESTAMP
  • TIMESTAMP WITH TIME ZONE

Oracle Data Definition

  • CREATE TABLE
  • Identity Column
  • ALTER TABLE
  • ALTER TABLE ADD Column
  • ALTER TABLE MODIFY Column
  • Drop Columns
  • DROP TABLE
  • TRUNCATE TABLE
  • RENAME Table
  • Oracle Virtual Column

Oracle Constraints

  • PRIMARY KEY
  • FOREIGN KEY
  • UNIQUE
  • CHECK
  • NOT NULL

Oracle Views

  • CREATE VIEW
  • DROP VIEW
  • Updatable Views
  • Inline Views
  • WITH CHECK OPTION

About Oracle Tutorial

OracleTututorial.com website provides Developers and Database Administrators with the updated Oracle tutorials, scripts, and tips.

Search

Recent Tutorials

  • Oracle Implicit Statement Results
  • Calling PL/SQL Stored Functions in Python
  • Calling PL/SQL Procedures in Python
  • Managing Transaction in Python
  • Deleting Data From Oracle Database in Python

Site Links

  • Oracle Books
  • About
  • Contact
  • Privacy Policy
  • Terms of Use

Copyright © 2021 Oracle Tutorial. All Rights Reserved.