file:///P:/Oracle官方文档/11.2官方文档/server.112/e26088/functions119.htm#SQLRF00684
NVL
Syntax
Description of the illustration nvl.gif
NVL lets you replace null (returned as a blank) with a string in the results of a query. Ifexpr1 is null, thenNVL returnsexpr2. Ifexpr1 is not null, thenNVL returnsexpr1.
The arguments expr1 and expr2 can have any data type. If their data types are different, then Oracle Database implicitly converts one to the other. If they cannot be converted implicitly, then the database returns an error. The implicit conversion is implemented as follows:
-
If
expr1is character data, then Oracle Database convertsexpr2to the data type ofexpr1before comparing them and returnsVARCHAR2in the character set ofexpr1. -
If
expr1is numeric, then Oracle Database determines which argument has the highest numeric precedence, implicitly converts the other argument to that data type, and returns that data type.See Also:
-
Table 3-10, "Implicit Type Conversion Matrix" for more information on implicit conversion and"Numeric Precedence" for information on numeric precedence
-
"COALESCE" and"CASE Expressions", which provide functionality similar to that of
NVL
-
The following example returns a list of employee names and commissions, substituting "Not Applicable" if the employee receives no commission:
SELECT last_name, NVL(TO_CHAR(commission_pct), 'Not Applicable') commission FROM employees WHERE last_name LIKE 'B%' ORDER BY last_name; LAST_NAME COMMISSION ------------------------- ---------------------------------------- Baer Not Applicable Baida Not Applicable Banda .1 Bates .15 Bell Not Applicable Bernstein .25 Bissot Not Applicable Bloom .2 Bull Not Applicable
SQL> create table testNVL(
2 id number,
3 tName varchar2(20)
4 );
Table created.
SQL> insert into testnvl(id,tname) values(1,'1');
1 row created.
SQL> insert into testnvl(id) values(2);
1 row created.
SQL> insert into testnvl(tname) values('3');
1 row created.
SQL> insert into testnvl(id,tname) values('4','4');
1 row created.
SQL> select id,tname from testnvl;
ID TNAME
---------- --------------------
1 1
2
3
4 4
SQL> select id,tname from testnvl;
ID TNAME
---------- --------------------
1 1
2
3
4 4
SQL> select id,nvl(id,10*id),tname from testnvl;
ID NVL(ID,10*ID) TNAME
---------- ------------- --------------------
1 1 1
2 2
3
4 4 4
SQL> select id,tname,nvl(tname,10*tname) from testnvl;
ID TNAME NVL(TNAME,10*TNAME)
---------- -------------------- ----------------------------------------
1 1 1
2
3 3
4 4 4
SQL> select id,nvl(id,'is null'),tname from testnvl;
select id,nvl(id,'is null'),tname from testnvl
*
ERROR at line 1:
ORA-01722: invalid number
SQL> select id,tname,nvl(tname,'is null') from testnvl;
ID TNAME NVL(TNAME,'ISNULL')
---------- -------------------- --------------------
1 1 1
2 is null
3 3
4 4 4
SQL> select id,nvl(to_char(id),'is null'),tname from testnvl;
ID NVL(TO_CHAR(ID),'ISNULL') TNAME
---------- ---------------------------------------- --------------------
1 1 1
2 2
is null 3
4 4 4

6518

被折叠的 条评论
为什么被折叠?



