ORACLE数据隐式转换规则 _MySQL, Oracle及数据库讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  MySQL, Oracle及数据库讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 1840 | 回复: 0   主题: ORACLE数据隐式转换规则         下一篇 
jrong1987
注册用户
等级:新兵
经验:66
发帖:67
精华:0
注册:2011-12-23
状态:离线
发送短消息息给jrong1987 加好友    发送短消息息给jrong1987 发消息
发表于: IP:您无权察看 2015-7-1 10:13:23 | [全部帖] [楼主帖] 楼主

在oracle中,如果不同的数据类型之间关联,如果不显式转换数据,则它会根据以下规则对数据进行隐式转换:

The following rules govern the direction in which Oracle makes implicit datatype conversions:
1) During INSERT and UPDATE operations, Oracle converts the value to the datatype of the affected column.


对于INSERT和UPDATE操作,oracle会把插入值或者更新值隐式转换为字段的数据类型。如
假如id列的数据类型为number
update t set id='1'; -> 相当于 update t set id=to_number('1');

insert into t(id) values('1') -> insert into t values(to_number('1'));
2) During SELECT FROM operations, Oracle converts the data from the column to the type of the target variable.


对于SELECT语句,oracle会把字段的数据类型隐式转换为变量的数据类型。如
假设id列的数据类型为varchar2

select * from t where id=1; -> select * from t where to_number(id)=1;


但如果id列的数据类型为number,则

select * from t where id='1'; -> select * from t where id=to_number('1');(参考下文)
3) When comparing a character value with a NUMBER value, Oracle converts the character data to NUMBER.


当比较一个字符型和数值型的值时,oracle会把字符型的值隐式转换为数值型。如
假设id列的数据类型为number

select * from t where id='1'; -> select * from t where id=to_number('1');
4) When comparing a character value with a DATE value, Oracle converts the character data to DATE.


当比较字符型和日期型的数据时,oracle会把字符型转换为日期型。如假设create_date为字符型,
select * from t where create_date>sysdate; -> select * from t where to_date(create_date)>sysdate;(注意,此时session的nls_date_format需要与字符串格式相符)假设create_date为date型,

select * from t where create_date>'2006-11-11 11:11:11'; -> select * from t where create_date>to_date('2006-11-11 11:11:11'); (注意,此时session的nls_date_format需要与字符串格式相符)
5) When you use a SQL function or operator with an argument of a datatype other than the one it accepts,
Oracle converts the argument to the accepted datatype.


如果调用函数或过程等时,如果输入参数的数据类型与函数或者过程定义的参数数据类型不一直,则oracle会把输入参数的数据类型转换为函数或者过程定义的数据类型。如
假设过程如下定义 

p(p_1 number)
exec p('1'); -> exec p(to_number('1'));
6) When making assignments, Oracle converts the value on the right side of the equal sign (=) to the datatype of the target of the assignment on the left side.


赋值时,oracle会把等号右边的数据类型转换为左边的数据类型。如

var a number
a:='1'; - > a:=to_number('1');
7) During concatenation operations, Oracle converts from noncharacter datatypes to CHAR or NCHAR.


用连接操作符(||)时,oracle会把非字符类型的数据转换为字符类型。

select 1||'2' from dual; -> select to_char(1)||'2' from dual;
8) During arithmetic operations on and comparisons between character and noncharacter datatypes,
Oracle converts from any character datatype to a number, date, or rowid, as appropriate.
In arithmetic operations between CHAR/VARCHAR2 and NCHAR/NVARCHAR2, Oracle converts to a number.


如果字符类型的数据和非字符类型的数据(如number、date、rowid等)作算术运算,则oracle会将字符类型的数据转换为合适的数据类型,这些数据类型可能是number、date、rowid等。如果CHAR/VARCHAR2 和NCHAR/NVARCHAR2之间作算术运算,则oracle会将她们都转换为number类型的数据再做比较。

9) Comparisons between CHAR/VARCHAR2 and NCHAR/NVARCHAR2 types may entail different character sets. 

The default direction of conversion in such cases is from the database character set to the national character set.


比较CHAR/VARCHAR2 和NCHAR/NVARCHAR2时,如果两者字符集不一样,则默认的转换方式是将数据编码从数据库字符集转换为国家字符集。

简单总结:
比较时,一般是字符型转换为数值型,字符型转换为日期型。算术运算时,一般把字符型转换为数值型,字符型转换为日期型。连接时(||),一般是把数值型转换为字符型,日期型转换为字符型。赋值、调用函数时,以定义的变量类型为准。

--转自 北京联动北方科技有限公司




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论