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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM