从带有数值的 varchar2 列中选择有时会出现无效数字错误
我试图理解为什么在某些系统上,当我尝试从 varchar2 列中选择一个值时,我会收到无效数字错误消息,而在其他系统上,我在执行完全相同的操作时不会收到错误消息。
该表是这样的:
ID Column_1 Column_2
1 V text
2 D 1
3 D 2
4 D 3
和一个查询:
select ID
from table
where column_1='D'
and column_2 = :some_number_value
:some_number_value 始终是数字,但可以为空。
我们修复了该查询:
select ID
from table
where column_1='D'
and column_2 = to_char(:some_number_value)
这个原始查询在大多数系统上运行良好,但在某些系统上会出现“无效数字”错误。问题是为什么?为什么它在大多数系统上工作而在某些系统上不起作用?
I'm trying to understand why, on some systems, I get an invalid number error message when I'm trying to select a value from a varchar2 column while on other systems I don't get the error while doing the exact same thing.
The table is something like this:
ID Column_1 Column_2
1 V text
2 D 1
3 D 2
4 D 3
and a query:
select ID
from table
where column_1='D'
and column_2 = :some_number_value
:some_number_value is always numeric but can be null.
We've fixed the query:
select ID
from table
where column_1='D'
and column_2 = to_char(:some_number_value)
This original query runs fine on most systems but on some systems gives an "invalid number" error. The question is why? Why does it work on most systems and not on some?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这取决于检查您的条件的顺序。如果首先评估
column_1
的条件,则将column_2
隐式转换为数字。如果首先检查column_2
,则将文本
转换为数字会失败。优化器决定评估条件的顺序。例如,如果您在
column_2
上有索引,但在column_1
上没有索引,则可能会使用该索引,因此转换将会失败。“some_number_value 始终是数字但可以为空”是什么意思?检查
column_2 = NULL
不会返回任何行,NULL
只能使用IS NULL
进行评估,而不能使用= NULL
代码>.This dependes on the order in which your conditions are checked. If the condition for
column_1
is evaluated first, then the implicit conversion ofcolumn_2
to a number works. Ifcolumn_2
is checked first, then the conversion oftext
to a number fails.The optimizer decides in which order the conditions are evaluated. If you have an index on
column_2
but not oncolumn_1
for example, this index is probably going to be used, so the conversion will fail.What do you mean by "some_number_value is always numeric but can be null"? The check for
column_2 = NULL
will return no rows,NULL
can only be evaluated by usingIS NULL
, not= NULL
.