赞
踩
oracle对不同数据类型具有显示转换和隐式转换2种。
Oracle官方建议使用显示转换,原因包括以下几点:
11.2.0.4隐式转换表格:
隐式转换的规则:
看完规则有以下几个疑问:
- SQL> create table lzlimpl(char1 char(100),date1 date,number1 number,timestamp1 timestamp);
-
-
- Table created.
-
-
- SQL> insert into lzlimpl values('2018-12-10 14:17:00',sysdate,1,sysdate);
-
-
- 1 row created.
-
- --这里已经出现了timestamp隐式转换date
很明显filter那里显示了隐式转换的方式
使用的不是to_date,而是internal_function
- SQL> select * from lzlimpl where char1=number1;
- select * from lzlimpl where char1=number1
- *
- ERROR at line 1:
- ORA-01722: invalid number
- --char1中的数据不能直接转换为number,会报ORA-01722
- SQL> delete from lzlimpl where char1='2018-12-10 14:17:00';
-
- 1 row deleted.
- SQL> insert into lzlimpl values('1',sysdate,1,sysdate);
-
- 1 row created.
- SQL> commit
- 2 /
-
- Commit complete.
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
很明显是char转换为number
SQL> select * from lzlimpl where date1=timestamp1;
这里也出现了internal_function,date转换为了timestamp,oracle往更高的精度转换。
从这个internal_function看,这个函数不仅可以把char转换成了date,还可以把date转换成timestamp,应该还有更多功能
- SQL> truncate table lzlimpl;
-
-
- Table truncated.
-
-
- SQL> insert into lzlimpl values('1.234',sysdate,1,sysdate);
-
-
- 1 row created.
-
- SQL> select char1*number1 from lzlimpl;
-
-
- CHAR1*NUMBER1
- -------------
-
- 1.234
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
第二条规则
肯定是做了隐式转换,但是这样看不出来。换一种方式
- SQL> select * from lzlimpl where char1*number1=1.2345678;
-
-
-
- no rows selected
为了计算,只能char转换为number,不同精准度的数据也可以计算
因为char补全了空格,所以no rows
char和varchar2
char和varchar可以直接对比没有隐式转换
char需要转换为nchar
char类型的转换总结:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。