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

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. Write a query to get 2nd…

'Linux-x86_64 Error: 28: No space left on device' While trying to start the database -- Error

SQL> startup mount pfile='/tmp/initdlfasp12.ora'
ORA-27102: out of memory
Linux-x86_64 Error: 28: No space left on device

This as you can see is on Linux x86 with 64 bit processor. We got this error after we changed SGA on 10gR2 database. So was sure that this is something to do with the OS.

Parameters to check for this are shmall.

shmall is the total amount of shared memory, in pages, that the system can use at one time.

$cat /proc/sys/kernel/shmmax

$ getconf PAGE_SIZE

As per Oracle SHMALL should be set to the total amount of physical RAM divided by page size.

Our system has 64GB memory, so change kernel.shmall = 1024 * 1024 * 1024 * 64 / 4096 = 16777216

Once this value is calculated you can modify Linux system configuration file directly.

$ su - root
vi /etc/sysctl.conf file:


# sysctl -p

Once this is done the database was started without any problem.

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;


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> SELECT empno, ename…