NVL Syntax
Syntax : NVL ( expr1, expr2 )
-- DATE 데이터 타입
SQL> SELECT NVL(c2, '01/JAN/2011') as "Date" FROM NULL_T
2 WHERE c2 is null AND rownum <= 1 ;
Date
---------
01-JAN-11
SQL> ALTER SESSION SET nls_date_format = 'DD/MON/YYYY' ;
Session altered.
-- Charater 데이터 타입
SQL> SELECT NVL(c2, '01/JAN/2011') as "Date" FROM NULL_T
2 WHERE c2 is null AND rownum <= 1 ;
Date
-----------
01/JAN/2011
SQL> SELECT NVL(c3, 'Null Data') AS "Character" FROM NULL_T
2 WHERE c3 IS NULL AND ROWNUM <= 1 ;
Character
----------
Null Data
-- Number 데이터 타입
SQL> SELECT NVL(c3, 100) AS "Character" FROM NULL_T
2 WHERE c3 IS NULL AND ROWNUM <= 1 ;
Character
----------
100
SQL> SELECT NVL(c4, 'Null Data') AS "Number" FROM null_t
2 WHERE c4 IS NULL AND ROWNUM <= 1 ;
SELECT NVL(c4, 'Null Data') AS "Number" FROM null_t
*
ERROR at line 1:
ORA-01722: invalid number
SQL> SELECT NVL(c4, '10') AS "Number" FROM NULL_T
2 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
NVL2 Syntax
Syntax : NVL2 (expr1, expr2, expr3)
SQL> WITH null_t_temp AS ( SELECT c3
2 FROM NULL_T
3 WHERE c3 IS NULL AND ROWNUM <= 1
4 UNION ALL
5 SELECT c3
6 FROM NULL_T
7 WHERE c3 IS NOT NULL AND ROWNUM <= 1 )
8 SELECT c3, NVL2(c3, 'Not Null', 'Null') AS Return_Data
9 FROM null_t_temp ;
C3 RETURN_D
---------- --------
Null
01-APR-00 Not Null
SQL> WITH null_t_temp AS ( SELECT c3
2 FROM NULL_T
3 WHERE c3 IS NULL AND ROWNUM <= 1
4 UNION ALL
5 SELECT c3
6 FROM NULL_T
7 WHERE c3 IS NOT NULL AND ROWNUM <= 1 )
8 SELECT c3, NVL2(c3, 100, 100) AS Return_Data
9 FROM null_t_temp ;
C3 RETURN_DATA
---------- -----------
100
01-APR-00 100
SQL> WITH null_t_temp AS ( SELECT c3
2 FROM NULL_T
3 WHERE c3 IS NULL AND ROWNUM <= 1
4 UNION ALL
5 SELECT c3
6 FROM NULL_T
7 WHERE c3 IS NOT NULL AND ROWNUM <= 1 )
8 SELECT c3, NVL2(c3, 100, 'Null') AS Return_Data
9 FROM null_t_temp ;
SELECT c3, NVL2(c3, 100, 'Null') AS Return_Data
*
ERROR at line 8:
ORA-01722: invalid number
SQL> WITH null_t_temp AS ( SELECT c3
2 FROM NULL_T
3 WHERE c3 IS NULL AND ROWNUM <= 1
4 UNION ALL
5 SELECT c3
6 FROM NULL_T
7 WHERE c3 IS NOT NULL AND ROWNUM <= 1 )
8 SELECT c3, NVL2(c3, 100, '95') AS Return_Data
9 FROM null_t_temp ;
C3 RETURN_DATA
---------- -----------
95
01-APR-00 100
SQL> WITH null_t_temp AS ( SELECT c3
2 FROM NULL_T
3 WHERE c3 IS NULL AND ROWNUM <= 1
4 UNION ALL
5 SELECT c3
6 FROM NULL_T
7 WHERE c3 IS NOT NULL AND ROWNUM <= 1 )
8 SELECT c3, NVL2(c3, 'Not Null', 100) AS Return_Data
9 FROM null_t_temp ;
C3 RETURN_D
---------- --------
100
01-APR-00 Not Null