首页 > 代码库 > Oracle NULL相关函数
Oracle NULL相关函数
一、NVL
官方解释:
Purpose
NVL lets you replace null (returned as a blank) with a string in the results of a query.
If expr1 is null, then NVL returns expr2. Ifexpr1 is not null, then NVL returns expr1.
如果expr1是null,则返回expr2,如果expr1 is not null,则返回expr1.
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.
expr1 and expr2 可以是任意的数据类型,但他们必须是同一数据类型,或者是隐式转换为同一数据类型,又或者是显示转换为同一数据类型。
如果他们不是同一类型,则报错。
The implicit conversion is implemented as follows:
- If expr1 is character data, then Oracle Database converts expr2 to the data type ofexpr1 before comparing them and returns VARCHAR2 in the character set ofexpr1.
- 如果expr1 是字符类型,则expr2在比较前转换为expr1的数据类型,在进行比较。
- If expr1 is 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.
- 如果expr1是数字类型,则判断哪个参数的数据类型高就隐式转为哪个数据类型。
官方参考:http://docs.oracle.com/cd/E11882_01/server.112/e41084/functions119.htm#sthref1312
二、NVL2
Purpose
NVL2 lets you determine the value returned by a query based on whether a specified expression is null or not null.
If expr1 is not null, then NVL2 returns expr2. If expr1 is null, then NVL2 returns expr3.
如果expr1非空,则返回 expr2,如果expr1是空值,则返回expr3
The argument expr1 can have any data type. The arguments expr2 and expr3 can have any data types except LONG.
expr1 可以是任意数据类型, expr2 and expr3 可以是任意数据类型,但不能是LONG类型,且数据类型要一致,或者隐式转换为一致,或者显示转换为一致。
If the data types of expr2 and expr3 are different, then Oracle Database implicitly converts one to the other.
如果expr2 and expr3 数据类型不同,则隐式转为相同
If they cannot be converted implicitly, then the database returns an error.
如果不能隐式转换,则报错。
If expr2 is character or numeric data, then the implicit conversion is implemented as follows:
- If expr2 is character data, then Oracle Database converts expr3 to the data type of expr2 before returning a value unless expr3 is a null constant.
- In that case, a data type conversion is not necessary, and the database returns VARCHAR2 in the character set of expr2.
- 如果expr2 是字符类型,则将expr3 转换为expr2相同的数据类型。
- If expr2 is numeric data, 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.
官方参考:http://docs.oracle.com/cd/E11882_01/server.112/e41084/functions120.htm#sthref1315
三、NULLIF
Purpose
NULLIF compares expr1 and expr2. If they are equal, then the function returns null.
比较expr1 and expr2,如果相等,则返回null值。
If they are not equal, then the function returns expr1. You cannot specify the literal NULL for expr1.
如果不等,则返回expr1 。不能指定expr1为空。
If both arguments are numeric data types, then Oracle Database determines the argument with the higher numeric precedence, implicitly converts the other argument to that data type, and returns that data type.
If the arguments are not numeric, then they must be of the same data type, or Oracle returns an error.
如果参数类型不是数字类型,则必须是相同数据类型,否则报错。
The NULLIF function is logically equivalent to the following CASE expression:
CASE WHEN expr1 = expr2 THEN NULL ELSE expr1 END
官网参考:http://docs.oracle.com/cd/E11882_01/server.112/e41084/functions116.htm#sthref1303
四、COALESCE
Purpose
COALESCE (expression_1, expression_2, ...,expression_n)
列表中第一个非空的表达式是函数的返回值,如果所有的表达式都是空值,最终将返回一个空值。
COALESCE returns the first non-null expr in the expression list. You must specify at least two expressions. If all occurrences of expr evaluate to null, then the function returns null.
Oracle Database uses short-circuit evaluation. The database evaluates each expr value and determines whether it is NULL, rather than evaluating all of the expr values before determining whether any of them is NULL.
If all occurrences of expr are numeric data type or any nonnumeric data type that can be implicitly converted to a numeric data type, then Oracle Database determines the argument with the highest numeric precedence, implicitly converts the remaining arguments to that data type, and returns that data type.
This function is a generalization of the NVL function.
You can also use COALESCE as a variety of the CASE expression. For example,
COALESCE(expr1, expr2)
is equivalent to:
CASE WHEN expr1 IS NOT NULL THEN expr1 ELSE expr2 END
Similarly,
COALESCE(expr1, expr2, ..., exprn)
where n >= 3, is equivalent to:
CASE WHEN expr1 IS NOT NULL THEN expr1
ELSE COALESCE (expr2, ..., exprn) END
官方参考:http://docs.oracle.com/cd/E11882_01/server.112/e41084/functions030.htm#sthref995
五、DECODE
DECODE(expr,search1,result1[,search2,result2……,default]):
比较expr与search,如果等于search1则返回result1,如果等于search2则返回result2,依次类推,如果都不等于,如果有default则返回default,否则返回NULL.
ORACLE在比较之前,会自动把expr和每一个search隐式转换成第一个search(search1)的数据类型。自动把返回值转换成第一个result(result1)的数据类型。如果第一个result的数据类型为CHAR或者值是null,则Oracle转换返回值为VARCHAR2.
在DECODE函数中,NULL是相等的,如果expr为空,则Oracle将会返回第一个为NULL的search所对应的result。DECODE列表中的最大表达式个数为255个。
expr与search1同类型,searchn与searche1同类型
返回值与result1同类型
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。