Call now: 252-767-6166  
Oracle Training Oracle Support Development Oracle Apps

 
 Home
 E-mail Us
 Oracle Articles
New Oracle Articles


 Oracle Training
 Oracle Tips

 Oracle Forum
 Class Catalog


 Remote DBA
 Oracle Tuning
 Emergency 911
 RAC Support
 Apps Support
 Analysis
 Design
 Implementation
 Oracle Support


 SQL Tuning
 Security

 Oracle UNIX
 Oracle Linux
 Monitoring
 Remote s
upport
 Remote plans
 Remote
services
 Application Server

 Applications
 Oracle Forms
 Oracle Portal
 App Upgrades
 SQL Server
 Oracle Concepts
 Software Support

 Remote S
upport  
 Development  

 Implementation


 Consulting Staff
 Consulting Prices
 Help Wanted!

 


 Oracle Posters
 Oracle Books

 Oracle Scripts
 Ion
 Excel-DB  

Don Burleson Blog 


 

 

 


 

 

 
 

Oracle NVL tips

Oracle Database Tips by Donald BurlesonJuly 13, 2015

Question:  I have a table where a NULL department sorts to the end of my list.  I want the NULL column to sort as if it was a blank space.  Do I change my ORDER BY clause, or is there another way to transform a NULL value?

Answer:  You have two options here:

  1. You can change the sort order of a NULL using an order by NULLS first.
     
  2. Use also use the NVL clause

If you want to change the NULL to a real low value you the NULL VALUE (NVL clause).  This example use the NVL function to change a NULL to a blank space:

select
   name,
   nvl (dept_id,' ') c2
from
   mytab
order by
   name,
   c2;

The Oracle NULL Value (NVL) SQL operator is a great way to substitute NULL values with numeric values in Oracle SQL statements.

The most common use for the NULL value (NVL) clause is to prevent mathematical operations from aborting with a zero divisor. Here are examples of the NVL SQL operator:
 
select nvl(b.buffer_gets,0) -- NVL replaces NULL value with a zero
 
select nvl(current_status, "Not disclosed") -- NVL replaces a NULL value with a string
 
As you can see, the NVL function replaces a NULL with whatever value you desire.

The nvl function only has two parameters while the nvl parameter has three arguments.  The nvl2 like like combining an nvl with a decode because you can transform a value:

  • NVL ( expr1 , expr2 ): If expr1 is null, then NVL returns expr2. If expr1 is not null, then NVL returns expr1.

  • NVL2 ( expr1 , expr2 , expr3 ): If expr1 is null, then NVL2 returns expr3. If expr1 is not null, then NVL2 returns expr2

As we see, the vanilla nvl transformation takes a NULL value and replaces it with a printable, useable value, such as a zero or spaces:

select nvl(b.buffer_gets,0) - NVL replaces a NULL value with a zero
select nvl(current_status, ?Not disclosed?) - NVL replaces a NULL value with a string

Conversely, the NVL2 clause accepts three arguments, but ALWAYS transforms the input argument. 

select NVL2(supplier_city, 'Completed', 'n/a') from suppliers;

In this example, these statements are equivalent because the nvl2 re-sets the input argument back to the original value:

select nvl(commission_pct,0) 2 from employees;

select nvl2(commission_pct,commission_pct,0) 2 from employees;

Using the Null-Value Clause with Oracle SQL

The null value clause of Oracle SQL arises from the three valued logic that is the foundation of the SQL language.  Essentially, three values logic understands that a missing value (NULL inside Oracle), is NOT the same as the default for that data type.  For example, a NULL salary for an employee is not the same thing as a salary of zero.
Hence, we need to be able to display data from Oracle tables, and quickly convert null values

SELECT
   ename,
   NVL(TO_CHAR(SALARY), 'NOT YET ASSIGNED') "SALARY"
FROM
   employee; 

ENAME SALARY
---------- -------------------------------------
BARRETT         85,000
BURLESON     101,000
DAHLSTROM NOT YET ASSIGNED

Now, let's look at another important application of the NVL BIF.

The outer join problem with NULL values

The null value issue is even more problematic when two tables are joined via an "outer join".  In an outer join, we have a condition where two tables are joined together but there may not always be a corresponding row in both tables.  When an outer join is requested, Oracle will display all of the rows of the joining table, even if there is not a matching row in the other table.  For these rows, we must handle the NULL values that are returned by the Oracle query.

 

Also see these notes on advanced NULL operators in Oracle SQL.

Remember, that you can create an index on NULL values, using the nvl operator


Also see these notes on using the nvl clause:



 

 

��  
 
 
Oracle Training at Sea
 
 
 
 
oracle dba poster
 

 
Follow us on Twitter 
 
Oracle performance tuning software 
 
Oracle Linux poster
 
 
 

 

Burleson is the American Team

Note: This Oracle documentation was created as a support and Oracle training reference for use by our DBA performance tuning consulting professionals.  Feel free to ask questions on our Oracle forum.

Verify experience! Anyone considering using the services of an Oracle support expert should independently investigate their credentials and experience, and not rely on advertisements and self-proclaimed expertise. All legitimate Oracle experts publish their Oracle qualifications.

Errata?  Oracle technology is changing and we strive to update our BC Oracle support information.  If you find an error or have a suggestion for improving our content, we would appreciate your feedback.  Just  e-mail:  

and include the URL for the page.


                    









Burleson Consulting

The Oracle of Database Support

Oracle Performance Tuning

Remote DBA Services


 

Copyright © 1996 -  2020

All rights reserved by Burleson

Oracle ® is the registered trademark of Oracle Corporation.