-
Syntax : NVL(expr1, expr2)
-
SQL> SELECT NVL(c2, '01/JAN/2011') as "Date"
FROM NULL T
WHERE c2 is null
AND rownum <= 1;
ERROR at line 1:
ORA-01858: a non-numeric character was found where a numeric was expected
SQL> ALTER SESSION SET nls_date_format = 'DD/MON/YYYY';
SQL> SELECT NVL(c2, '01/JAN/2011' ) as "Date"
FROM NULL_T
WHERE c2 is null
AND rownum <= 1;
Date
------------
01-JAN-11
SQL> SELECT NVL(c3, 'Null Data') AS "Character"
FROM NULL_T
WHERE c3 iS NULL
AND ROWNUM <= 1;
Character
--------------------
Null Data
SQL> SELECT NVL(c3, 100) AS "Character"
FROM NULL_T
WHERE c3 iS NULL
AND ROWNUM <= 1;
Character
--------------------
100
SQL> SELECT NVL(c4, 'Null Data') AS "Number"
FROM NULL_T
WHERE c4 iS NULL
AND ROWNUM <= 1;
SELECT NVL(c4, 'Null Data') AS "Number"
*
ERROR at line 1:
ORA-01722: invalid number
SQL> SELECT NVL(c4, '10') AS "Number"
FROM NULL_T
WHERE c4 iS NULL
AND ROWNUM <= 1;
Number
----------
10
SQL> SELECT NVL(c4, 10) AS "Number"
FROM NULL_T
WHERE c4 iS NULL
AND ROWNUM <= 1;
Number
----------
10
--
Syntax : NVL2(expr1, expr2, expr3)
--
SQL> WITH null_t_temp AS (SELECT c3
FROM NULL_T
WHERE c3 iS NULL
AND ROWNUM <= 1
UNION ALL
SELECT c3
FROM NULL_T
WHERE c3 iS NOT NULL
AND ROWNUM <= 1)
SELECT c3
,NVL2(c3, 'Not Null', 'Null') AS Return_Data
FROM null_t_temp;
C3 RETURN_DATA
-------------------- ----------------
Null
15-SEP-11 Not Null
SQL> WITH null_t_temp AS (SELECT c3
FROM NULL_T
WHERE c3 iS NULL
AND ROWNUM <= 1
UNION ALL
SELECT c3
FROM NULL_T
WHERE c3 iS NOT NULL
AND ROWNUM <= 1)
SELECT c3
,NVL2(c3, 100, 100) AS Return_Data
FROM null_t_temp;
C3 RETURN_DATA
-------------------- -----------
100
15-SEP-11 100
SQL> WITH null_t_temp AS (SELECT c3
FROM NULL_T
WHERE c3 iS NULL
AND ROWNUM <= 1
UNION ALL
SELECT c3
FROM NULL_T
WHERE c3 iS NOT NULL
AND ROWNUM <= 1)
SELECT c3
,NVL2(c3, 100, 'Null') AS Return_Data
FROM null_t_temp;
,NVL2(c3, 100, 'Null') AS Return_Data
*
ERROR at line 11:
ORA-01722: invalid number
SQL> WITH null_t_temp AS (SELECT c3
FROM NULL_T
WHERE c3 iS NULL
AND ROWNUM <= 1
UNION ALL
SELECT c3
FROM NULL_T
WHERE c3 iS NOT NULL
AND ROWNUM <= 1)
SELECT c3
,NVL2(c3, 100, '95') AS Return_Data
FROM null_t_temp;
C3 RETURN_DATA
-------------------- -----------
95
15-SEP-11 100
SQL> WITH null_t_temp AS (SELECT c3
FROM NULL_T
WHERE c3 iS NULL
AND ROWNUM <= 1
UNION ALL
SELECT c3
FROM NULL_T
WHERE c3 iS NOT NULL
AND ROWNUM <= 1)
SELECT c3
,NVL2(c3, 'Not Null', 100) AS Return_Data
FROM null_t_temp;
C3 RETURN_DATA
-------------------- ----------------
100
15-SEP-11 Not Null