0.date 和 timestamp 說明
- date 類型能夠存儲年、月、日、世紀、時、分、秒,經常使用的格式為:
MM/DD/YYYY HH24:MI:SS或者MM-DD-YYYY HH24:MI:SS。date 只能精確到秒,不能精確到秒之內的時間,一般的時間計算足夠,但是在時間精度要求較高的場景下,date 類型就有點力不從心。 - 為了解決 date 類型粒度不夠的問題,Oracle 擴展出了 timestamp 時間戳類型,timestamp 類型引入了小數秒的信息。
在 Oracle 中可以使用 sysdate 和 systimestamp 查詢當前系統時間、時間戳。
select sysdate from dual; -- 1 2019/12/24 14:59:37
select systimestamp from dual; -- 1 24-12月-19 03.00.29.523851 下午 +08:00
1.date 轉換為 timestamp
date 轉換為 timestamp 格式可以使用函數 to_timestamp 進行轉換。
select to_timestamp(sysdate) from dual;
-- 1 24-12月-19 12.00.00 上午
select to_timestamp('2019-12-24 14:57:10.1','yyyy-mm-dd hh24:mi:ss.ff') from dual;
-- 1 24-12月-19 02.57.10.100000000 下午
2.timestamp 轉換為 date
timestamp + 0
select systimestamp + 0 from dual;
-- 1 2019/12/24 15:01:54
to_char + to_date 方式
select to_char(systimestamp, 'yyyymmdd') from dual;
-- 1 20191224
-- 多加一層 to_date 轉化
cast 方式
select cast(systimestamp as date) from dual;
-- 1 2019/12/24 15:03:33
