22.oracle中日期类型 to_date 和to_timestamp什么区别

1、to_date() 和to_timestamp()区别

  由于oracle中date类型只支持到秒,不支持到毫秒,所以to_date()不能取到毫秒。如果要取到毫秒,oracle 9i以上版本,可以使用timestamp类型,

timestamp是date的扩展类型,能支持到毫秒,毫秒的显示精度是6位,不过有效位是3位,即最大值达到999,满1000ms就进为1s。

而与to_date()对应的转换函数可以使用to_timestamp()。两个date相减得到是两个时间的间隔,单位是天,两个timestamp相减的话,不能直接的得到天数,

而是得到多少天,多少小时,多少秒,多少毫秒等

1)获取小数点后6位的日期

-- 获取小数点后6位 --
select to_char(systimestamp,'yyyy-mm-dd hh24:mi:ss.ff6') from dual;

22.oracle中日期类型 to_date 和to_timestamp什么区别_第1张图片

2)字符串转换成timestamp型

--日期字符串转换成timestamp --
select to_timestamp('2018-10-31 12:52:42.1234567','yyyy-mm-dd hh24:mi:ss.ff') from dual;

22.oracle中日期类型 to_date 和to_timestamp什么区别_第2张图片

3)timestamp转换成date型

--3)timestamp转换成date
select cast(to_timestamp('2018-10-31 12:52:42.1234567','yyyy-mm-dd hh24:mi:ss.ff') as date) from dual; 

22.oracle中日期类型 to_date 和to_timestamp什么区别_第3张图片

4)date转换成timestamp型

--4)date转换成timestamp
select cast(to_date('2018-10-31 12:52:42','yyyy-mm-dd hh24:mi:ss') as timestamp) from dual; 

你可能感兴趣的:(Oracle数据库学习,oracle,数据库)