我可以在 Oracle 中为 varchar2 传递一个数字吗? [英] Can I pass a number for varchar2 in Oracle?

查看:65
本文介绍了我可以在 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.

这篇关于我可以在 Oracle 中为 varchar2 传递一个数字吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆