Oracle NVL 和 Coalesce 之间的差异
Oracle 中的 NVL 和 Coalesce 之间是否存在不明显的差异?
明显的区别是,coalesce 将返回其参数列表中的第一个非 null 项,而 nvl 仅接受两个参数,如果不为 null,则返回第一个参数,否则返回第二个。
看来 NVL 可能只是合并的“基本案例”版本。
我错过了什么吗?
Are there non obvious differences between NVL and Coalesce in Oracle?
The obvious differences are that coalesce will return the first non null item in its parameter list whereas nvl only takes two parameters and returns the first if it is not null, otherwise it returns the second.
It seems that NVL may just be a 'Base Case" version of coalesce.
Am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
COALESCE
是更现代的函数,是ANSI-92
标准的一部分。NVL
是Oracle
特定的,它是在80
出现任何标准之前引入的。如果有两个值,它们是同义词。
然而,它们的实现方式不同。
NVL
始终评估两个参数,而COALESCE
通常在发现第一个非NULL
时停止评估(有一些例外,例如序列 < code>NEXTVAL):它运行了近
0.5
秒,因为它生成了SYS_GUID()
,尽管1
不是一个NULL
。这可以理解
1
不是NULL
并且不会计算第二个参数。SYS_GUID
不会生成,并且查询是即时的。COALESCE
is more modern function that is a part ofANSI-92
standard.NVL
isOracle
specific, it was introduced in80
's before there were any standards.In case of two values, they are synonyms.
However, they are implemented differently.
NVL
always evaluates both arguments, whileCOALESCE
usually stops evaluation whenever it finds the first non-NULL
(there are some exceptions, such as sequenceNEXTVAL
):This runs for almost
0.5
seconds, since it generatesSYS_GUID()
's, despite1
being not aNULL
.This understands that
1
is not aNULL
and does not evaluate the second argument.SYS_GUID
's are not generated and the query is instant.NVL 将对第一个参数的数据类型进行隐式转换,因此以下内容不会出现
COALESCE 需要一致数据类型的错误。
将抛出“数据类型不一致错误”
NVL will do an implicit conversion to the datatype of the first parameter, so the following does not error
COALESCE expects consistent datatypes.
will throw a 'inconsistent datatype error'
NVL 和 COALESCE 用于实现相同的功能,即在列返回 NULL 时提供默认值。
区别在于:
argument
出现非空值。
给予它。 COALESCE 期望所有参数具有相同的数据类型。
下面的
以第三种情况为例。 其他情况都很简单。
select nvl('abc',10) from Dual;
将起作用,因为 NVL 会将数字 10 隐式转换为字符串。select coalesce('abc',10) from Dual;
将失败,并显示错误 - 不一致的数据类型:预期 CHAR 得到 NUMBERUNION 用例的示例
失败,并显示
ORA-00932: 不一致的数据类型:预期CHAR 获取 DATE
成功。
更多信息:http://www. plsqlinformation.com/2016/04/difference- Between-nvl-and-coalesce-in-oracle.html
NVL and COALESCE are used to achieve the same functionality of providing a default value in case the column returns a NULL.
The differences are:
arguments
occurrence of a non-Null value.
given to it. COALESCE expects all arguments to be of same datatype.
below
Examples for the third case. Other cases are simple.
select nvl('abc',10) from dual;
would work as NVL will do an implicit conversion of numeric 10 to string.select coalesce('abc',10) from dual;
will fail with Error - inconsistent datatypes: expected CHAR got NUMBERExample for UNION use-case
fails with
ORA-00932: inconsistent datatypes: expected CHAR got DATE
succeeds.
More information : http://www.plsqlinformation.com/2016/04/difference-between-nvl-and-coalesce-in-oracle.html
计划处理方面也存在差异。
当搜索包含
nvl
结果与索引列的比较时,Oracle 能够通过串联分支过滤器形成优化计划。nvl:
合并:
积分转到 http://www.xt-r.com/2012 /03/nvl-coalesce-concatenation.html。
There is also difference is in plan handling.
Oracle is able form an optimized plan with concatenation of branch filters when search contains comparison of
nvl
result with an indexed column.nvl:
coalesce:
Credits go to http://www.xt-r.com/2012/03/nvl-coalesce-concatenation.html.
Coalesce() 不会停止对第一个非空值进行求值的另一个证明:
运行此命令,然后检查
my_sequence.currval;
Another proof that coalesce() does not stop evaluation with the first non-null value:
Run this, then check
my_sequence.currval;
NVL:用值替换 null。
COALESCE:返回表达式列表中的第一个非空表达式。
表:PRICE_LIST
下面是
的示例
[1] 设定销售价格,所有产品加收10%利润。
[2] 如果没有购买标价,则销售价格为最低价格。 清仓出售。
[3] 如果也没有最低价格,则将销售价格设置为默认价格“50”。
用现实生活中的实际例子来解释。
您可以看到,使用 NVL 我们可以实现规则 [1],[2]
但是使用 COALSECE 我们可以实现所有三个规则。
NVL: Replace the null with value.
COALESCE: Return the first non-null expression from expression list.
Table: PRICE_LIST
Below is the example of
[1] Set sales price with adding 10% profit to all products.
[2] If there is no purchase list price, then the sale price is the minimum price. For clearance sale.
[3] If there is no minimum price also, then set the sale price as default price "50".
Explain with real life practical example.
You can see that with NVL we can achieve rules [1],[2]
But with COALSECE we can achieve all three rules.
尽管这一点是显而易见的,甚至在提出这个问题的汤姆提出的方式中也提到过。 但让我们再放一次。
NVL 只能有 2 个参数。 合并可能超过 2 个。
select nvl('','',1) from Dual;
//结果:ORA-00909
:参数数量无效select coalesce('','','1') from Dual;
//输出:返回1Though this one is obvious, and even mentioned in a way put up by Tom who asked this question. But lets put up again.
NVL can have only 2 arguments. Coalesce may have more than 2.
select nvl('','',1) from dual;
//Result:ORA-00909
: invalid number of argumentsselect coalesce('','','1') from dual;
//Output: returns 1其实我不能同意每一个说法。
“COALESCE 希望所有参数都具有相同的数据类型。”
这是错误的,见下文。 参数可以是不同的数据类型,这也有记录:< em>如果所有出现的 expr 都是数值数据类型或任何可以隐式转换为数值数据类型的非数值数据类型,则 Oracle 数据库确定具有最高数值优先级的参数,将其余参数隐式转换为该数据类型,并且返回该数据类型。。 实际上,这甚至与常见表达式“COALESCE 在第一次出现非空值时停止”相矛盾,否则测试用例 4 不应该引发错误。
另外,根据测试用例 5
COALESCE
执行参数的隐式转换。Actually I cannot agree to each statement.
"COALESCE expects all arguments to be of same datatype."
This is wrong, see below. Arguments can be different data types, that is also documented: 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.. Actually this is even in contradiction to common expression "COALESCE stops at first occurrence of a non-Null value", otherwise test case No. 4 should not raise an error.
Also according to test case No. 5
COALESCE
does an implicit conversion of arguments.