Definition:
NVL ( expr1 , expr2 )
If expr1 is null, then NVL returns expr2. If expr1 is not null, then NVL returns expr1.
If expr1 is null, then NVL returns expr2. If expr1 is not null, then NVL returns expr1.
Example:
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.
Now,
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. *
ERROR at line 1:
ORA-01722: invalid 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.
Definition:
NVL2 ( expr1 , expr2 , expr3 )
If expr1 is null, then NVL2 returns expr3. If expr1 is not null, then NVL2 returns expr2.
Example: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.
Comments
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.
2) Difference between 9i and 10g, more than 5 point?
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.
SQL> select 2a from emp;
why it take a as a column name and 2 as a value...
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 event.like oracle enable triggers to be invoked by many event other than table,insert,update and delete .