注:先转载一篇,但是本博文不是特别详细,抽时间博主整理个详细点的
TO_CHAR 是把日期或数字转换为字符串
TO_NUMBER 将字符转化为数字(1) TO_DATE
使用TO_DATE函数将字符转换为日期,规范:TO_DATE(char,‘格式’)
例1:
- selectto_date(‘2012-07-0110:00:00’,‘yyyy-mm-ddhh24:mi:ss’)fromdual;
- selectto_date(‘2012-07-0110:00’,‘yyyy-mm-ddhh24:mi’)fromdual;
- selectto_date(‘2012-07-0110’,‘yyyy-mm-ddhh24’)fromdual;
- selectto_date(‘2012-07-01’,‘yyyy-mm-dd’)fromdual;
- selectto_date(‘2012-07’,‘yyyy-mm’)fromdual;
- selectto_date(‘2012’,‘yyyy’)fromdual;
select to_date('2012-07-01 10:00:00','yyyy-mm-dd hh24:mi:ss') from dual; select to_date('2012-07-01 10:00','yyyy-mm-dd hh24:mi') from dual; select to_date('2012-07-01 10','yyyy-mm-dd hh24') from dual; select to_date('2012-07-01','yyyy-mm-dd') from dual; select to_date('2012-07','yyyy-mm') from dual; select to_date('2012','yyyy') from dual;
注意:yyyy/ yyy / yy分别代表4位/3位/2位的数字年,mm代表月,dd代表日,hh24 / hh12分别代表24小时制/ 12小时制,mi代表分,ss代表秒
例2:
表jmzs里的czsj是vachar2类型,转换成date类型:
- selectto_date(czsj,‘yyyy-mm-ddhh24:mi:ss’)fromjmzs;
select to_date(czsj,'yyyy-mm-dd hh24:mi:ss') from jmzs;
(2)TO_CHAR
使用TO_CHAR函数处理数字,规范:TO_CHAR(number,‘格式’)
使用TO_CHAR函数处理日期,规范:TO_CHAR(date,’格式’);
例1:
- selectTo_Char(to_date(‘2012-07-0110:00:00’,‘yyyy-mm-ddhh24:mi:ss’),‘yyyymmddhhmiss’)fromdual;
select To_Char(to_date('2012-07-01 10:00:00','yyyy-mm-dd hh24:mi:ss'),'yyyymmddhhmiss') from dual;
例2:
表jmzs里的czsj是vachar2类型,先转换成date类型,再转成char类型:
- selectTo_Char(to_date(czsj,‘yyyymmddhhmiss’)fromjmzs;
select To_Char(to_date(czsj,'yyyymmddhhmiss') from jmzs;
(3)TO_NUMBER
使用TO_NUMBER函数将字符转换为数字,规范:TO_NUMBER(char,‘格式’)
例1:
- selectTo_Number(To_Char(to_date(‘2012-07-0110:00:00’,‘yyyymmddhhmiss’))fromdual;
select To_Number(To_Char(to_date('2012-07-01 10:00:00','yyyymmddhhmiss')) from dual;
例2:
表jmzs里的czsj是vachar2类型,先转换成date类型,再转成char类型,最后才能转成number类型:
- selectTo_Number(To_Char(to_date(czsj,‘yyyymmddhhmiss’))fromjmzs;
select To_Number(To_Char(to_date(czsj,'yyyymmddhhmiss')) from jmzs;