Oracle系列:(9)三大类型转换
oracle中三大类型与隐式数据类型转换 (1)varchar2变长/char定长-->number,例如:'123'->123 (2)varchar2/char-->date,例如:'25-4月-15'->'25-4月-15' (3)number---->varchar2/char,例如:123->'123' (4)date------>varchar2/char,例如:'25-4月-15'->'25-4月-15' number<----->varchar2/char<----->date oracle如何隐式转换: 1)=号二边的类型是否相同 2)如果=号二边的类型不同,尝试的去做转换 3)在转换时,要确保合法合理,否则转换会失败,例如:12月不会有32天,一年中不会有13月 0、隐匿转换 查询1980年12月17日入职的员工(方式一:日期隐示式转换) select*fromempwherehiredate='17-12月-80'; 1、转向字符串 1.1、由日期转向字符串 使用to_char(日期,'格"常量"式')函数将日期转成字符串 显示如下格式:2015 年 04 月 25 日 星期六 selectto_char(sysdate,'yyyy"年"mm"月"dd"日"day')fromdual; 使用to_char(日期,'格式')函数将日期转成字符串,显示如格式:2015-04-25今天是星期六 15:15:15 selectto_char(sysdate,'yyyy-mm-dd"今天是"dayhh24:mi:ss')fromdual; 或 selectto_char(sysdate,'yyyy-mm-dd"今天是"dayHH12:MI:SSAM')fromdual; 1.2、由数值转向字符串 使用to_char(数值,'格式')函数将数值转成字符串,显示如下格式:$1,234 selectto_char(1234,'$9,999')fromdual; 使用to_char(数值,'格式')函数将数值转成字符串,显示如下格式:¥1,999')fromdual; selectto_char(1234,'L9,999')fromdual; 注意:L代表Locale的意思 2、由字符串转向日期 使用to_date('字符串','格式')函数,查询1980年12月17日入职的员工(方式二:日期显式转换) select*fromempwherehiredate=to_date('1980年12月17日','yyyy"年"mm"月"dd"日"'); 或 select*fromempwherehiredate=to_date('1980#12#17','yyyy"#"mm"#"dd'); 或 select*fromempwherehiredate=to_date('1980-12-17','yyyy-mm-dd'); 3、由字符串转向数值 使用to_number('字符串')函数将字符串‘123’转成数字123 selectto_number('123')fromdual; 注意: select'123'+123fromdual; 结果:246 select'123'||123fromdual; 结果:123123 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |