我可以在 Oracle 中为 varchar2 传递一个数字吗?
我有一个 Oracle 表和一个列 (col1
) 的类型为 varchar2(12 byte)
.它有一行,col1
的值为 1234
I have an Oracle table and a column (col1
) has type varchar2(12 byte)
. It has one row and value of col1
is 1234
当我说
select * from table where col1 = 1234
Oracle 说号码无效.这是为什么?为什么当它是 varchar2
时我不能传递数字?
Oracle says invalid number. Why is that? Why I cannot pass a number when it is varchar2
?
所有的回应都很棒.谢谢你.但是我不明白为什么当 1234
是一个有效的 varchar2 数据类型时它不需要 1234
.
All the responses are great. Thank you. But I am not able to understand why it does not take 1234
when 1234
is a valid varchar2 datatype.
问题是您希望 Oracle 将 1234 隐式转换为字符类型.相反,Oracle 将列隐式转换为数字.列中有一个非数字值,所以 Oracle 会抛出错误.Oracle 文档在解释之前警告不要进行隐式转换他们将如何解决.解释您所看到的行为的规则是:
The problem is that you expect that Oracle will implicitly cast 1234 to a character type. To the contrary, Oracle is implicitly casting the column to a number. There is a non-numeric value in the column, so Oracle throws an error. The Oracle documentation warns against implicit casts just before it explains how they will be resolved. The rule which explains the behaviour you're seeing is:
当比较字符值和数值时,Oracle 会将字符数据转换为数值.
When comparing a character value with a numeric value, Oracle converts the character data to a numeric value.