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相减的话,不能直接的得到天数,

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

-- 输出:364.626331018519 ---
select (sysdate-to_date('2017-11-01 00:00:00','yyyy-mm-dd hh24:mi:ss')) date相减 from dual;

-- 输出:+000000364 15:40:04.772000000 --
select (systimestamp-to_timestamp('2017-11-01 00:00:00','yyyy-mm-dd hh24:mi:ss')) timestamp相减 from dual;

结果视图:

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

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

结果视图

2)字符串转换成timestamp型

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

结果视图

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; 

结果视图

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; 

结果视图

参考网址:

http://www.cnblogs.com/hijushen/p/4223557.html

原文地址:https://www.cnblogs.com/xielong/p/9883705.html