[英]Oracle SQL export to csv show error: ORA-01722: invalid number
table_a table_b
-------------- ----------
product,amount,pcode rounding,pcode
-------------- ----------
apple,100.00,001 0.02,001
orange,150.02,001 -0.02,001
output
----------
apple,100.02
orange,150.00
select a.product||','||sum(a.amount)+b.rounding from table_a a,table_b b
where a.pcode=b.pcode
group by a.product,b.rounding
嗨,我试图将此查询假脱机为 csv 格式,但显示错误:
ORA-01722无效的号码。
当我删除 +b.rounding 然后根本没有发出时,我可以知道如何在我的查询中使用 sum(a.amount)+b.rounding 吗?
非常感谢您的帮助。
该错误与“导出到 csv”或聚合无关。 您可以非常简单地重现它,如下所示:
select 'a' || 1 + 3 as result from dual;
ORA-01722: invalid number
01722. 00000 - "invalid number"
*Cause: The specified number was invalid.
*Action: Specify a valid number.
原因很简单:连接( ||
运算符)与加法( +
)具有相同的优先级。 由于在您的公式连接中,首先执行它。 在我的示例中,数字1
被(隐式)转换为字符串'1'
并连接到'a'
,从而得到'a1'
。 然后这必须添加到3
。 为此,Oracle 尝试将'a1'
(隐式)转换为数字,显然这失败了,与您观察到的错误相同。
如果您希望结果是字符串'a4'
(在我的示例中),则解决方案很简单:使用括号。
select 'a' || (1 + 3) as result from dual;
RESULT
------
a4
这同样适用于你的情况。
请注意,以下作品 - 表明||
和+
具有相同的优先级; 连接并不比加法强,它们只是按顺序执行,从左到右。
select 1 + 3 || 'a' as result from dual;
RESULT
------
4a
这里我们不需要括号,因为 1 + 3 = 4,然后将其转换为字符串'4'
,然后将'a'
连接到它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.