暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Oracle 运算符优先级和隐式转换混淆

ASKTOM 2020-04-23
282

问题描述

使用sqlplus和APEX SQL命令与19.3.0.0.0版本的数据库我只是在玩简单的表达式,以确保我理解运算符优先级和隐式转换,并遇到这种情况,我无法理解。

为什么

select 8 || 1 + 2 * 7 || 9 from dual;


退货959

但是

select concat(8, 1 + 2 * 7) || 9 from dual;


返回8159,这是我对我在SQL语言参考中阅读的内容的理解,我希望得到的答案。



select 8 || ( 1 + 2 * 7 ) || 9 from dual;


返回8159

我在Live SQL上尝试了相同的方法,并获得了相同的结果。

问候并感谢您的宝贵时间。
-约翰

专家解答

从SQL引用中,优先级为:

PRECEDENCE

所以对于8 | | 1 2*7 | | 9

乘法是第一位的,所以:

8 | | 1 14 | | 9

但是现在我们有一个有趣的冲突...所有运算符现在都处于同等优先级,但是我们有一个数据类型问题,即,我们是否首先转换为字符串,以便允许串联,或者我们是否转换为数字以允许加法发生。

文档保持这种美好和钝 :-)

"Implicit Data Conversion
Oracle Database automatically converts a value from one data type to another when such a conversion makes sense.
Table 2-8 is a matrix of Oracle implicit conversions. The table shows all possible conversions, without regard to the direction of the conversion or the context in which it is made"


你的观察表明我们只是从左到右,所以: 8 | | 1变成to_char(8) | | to_char(1) 给出 '81'

Then '81 ”无法添加到14 (不匹配的数据类型),那么其to_number(81) 14,95

数字不能连接,所以它的to_char(95) | | to_char(9)
文章转载自ASKTOM,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论