Skip to main content

Difference between NVL and NVL2


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

Login as scott user and access table EMP.

First insert a row in table EMP using the following statement.

INSERT INTO EMP VALUES(321,'MITHUN',NULL,7782,sysdate,600,300,10);

In the above statement am trying to insert an employee MITHUN with job value NULL.


SELECT ename,job,NVL(JOB,'NO DESIGNATION') from emp;

Here first column is name, second is job and third column is again job with NVL function. For all other employees whose job is not null the value returned from NVL function is job but in case of ename MITHUN nvl returns the second argument that is 'NO DESIGNATION'.

Now consider the next statement,

SELECT ename,job,NVL(JOB,123) from emp;

In the above statement am trying to replace job with 123 in case if it is NULL.

This gets executed and 123 is displayed instead of NULL for ename MITHUN. This is because Oracle database does a implicit conversion of number to character.

Consider the next statement,

SELECT ename, comm, nvl(comm,9999) from emp;

Here am considering column comm which is of number datatype.

Here instead of returning 9999, try returning a character.

SELECT ename, comm, nvl(comm,'HELLO') from emp
ERROR at line 1:
ORA-01722: invalid number
This throws an error as database is not able to convert character into number.

NOTE: Number to character conversion is done implicitly by the database but Character to Number if the characters are not just numbers then database cannot convert this to numbers. Like 'HELLO' is a character which cannot be converted to number but '1234' though it is used inside single quotes, it is a number which will be converted.


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

SELECT ename, job, nvl2(job, 'YES', 123) FROM emp;

Here incase if job is not null then second argument 'YES' is returned but if the job is NULL then 123 is returned. Here second argument is character and third argument is a number. Database can convert third argument which is 123 to a character implicitly.

If you slightly change the order of argument like below,

SELECT ename, job, nvl2(job,123,'YES') FROM emp;

SELECT ename, job, nvl2(job,123,'YES') FROM emp
ERROR at line 1:
ORA-01722: invalid number

You would get an error as above. This is because database expects both the arguments in NVL2 to be of the same data type, incase of Character as argument2 and number as argument3, database converts argument3 to character to the data type of argument2 that is to character datatype.

But in case of argument2 is number and argument3 is character database cannot convert character to the number.


Smruti said…
Hi Sir Please give me the answer

Why we use @@ symbol in oracle
or difference between @ and @@

I read from the site but please give some easy example.

Sir I want to your sit looking as stack overflow site in which sliding capacity is available. post comment as post your answer just like yahoo in the header of the page so one can easily find the path and post a questions.
Smruti said…
1) Difference between constraint and trigger?

2) Difference between 9i and 10g, more than 5 point?
TARUN said…
I have install oracle 10 g express edition in my c drive.After installing my access rights to c drive is denied.Now there are two groups system and adminstrator while permission to system is allow using oracle only. what should I do neither I m able 2 uninstall the oracle nor able 2 make changes in rights through security settings of c to make it accessible.
Unknown said…
Sir I have a doubt in case of column alias
SQL> select 2a from emp;
why it take a as a column name and 2 as a value...
Manish kumar said…
Hello! Smuti
1) Difference between constraint and trigger?
ans-constraint are the restriction or condition that are used on columns of the table to preserve the data correctness.
and triggers provide a way of executing code on the occurrence of specific data oracle enable triggers to be invoked by many event other than table,insert,update and delete .

Popular posts from this blog

Basics of RDBMS

Data Small set of information becomes data, this set of information helps make decision. Data is always some useful information. Database Place where you store the data. Database represents some aspect of the real world called "miniworld". A database is designed, built and populated with data for a specific purpose. It has intended group of users and some preconceived applications in which these users are interested. In other words, a database has some source from which data is derived, some degree of interaction with events in the real world and an audience that is actively interested in the contents of the database. Database can also be defined as collection of one or more tables. Ex: Mobile, human brain etc DBMS (Database Management System ) Is a program that stores retrieves and modifies data in the database on request. Study of different techniques of design, development and maintenance of the database Types of DBMS These types are based upon their m

SQL Interview Questions on Subqueries

SUB Queries: 1. List the employees working in research department 2. List employees who are located in New York and Chicago 3. Display the department name in which ANALYSTS are working 4. Display employees who are reporting to JONES 5. Display all the employees who are reporting to Jones Manager 6. Display all the managers in SALES and ACCOUNTING department 7. Display all the employee names in Research and Sales Department who are having at least 1 person reporting to them 8. Display all employees who do not have any reportees 9. List employees who are having at least 2 reporting 10. List the department names which are having more than 5 employees 11. List department name having at-least 3 salesman 12. List employees from research and accounting having at-least 2 reporting 13. Display second max salary 14. Display 4th max salary 15. Display 5th max salary  -- Answer for nth Max Salary Co-Related Subqueries: 16. Write a query to get 4th max salary from EMP table 17. Wri

Answers for SQL Functions

1. SQL> SELECT empno, ename FROM emp WHERE Length(ename) = 4; 2. SQL> SELECT empno, ename, job FROM emp where Length(job)=7; 3. SQL> SELECT Length('qspiders') - Length(replace('qspiders','s','')) FROM dual; 4. SQL>  SELECT empno, ename, job FROM emp WHERE Instr(job,'MAN') >0; 5. SQL> SELECT empno, ename, job FROM emp WHERE Instr(job, 'MAN') =1; 6. SQL> SELECT empno, ename, job FROM emp WHERE (Length(ename) - Length(Replace(ename, 'L',''))) = 1; 7. SQL> SELECT * FROM dept WHERE Instr(dname,'O') > 0; 8. SQL> SELECT Concat(ename,' working as a ') || Concat(job, ' earns ') || Concat(sal, '  in ') || Conc at('dept ',deptno) AS text from emp; OR SQL> SELECT Concat(Concat(Concat(Concat(Concat(Concat(Concat(ename,' working as a '), job),' earns '), sal),'  in '),'dept '), deptno) AS text FROM emp; 9. SQL