Greenplum數據庫時間操作

Greenplum數據庫時間操作與mysql有一些區別,彙總以往筆記記錄下來。

  • greenplum時間格式:'yyyy-mm-dd hh24:mi:ss.us'、'yyyy-mm-dd hh:mi:ss.us'、'YYYY-DD-MM HH24:MI:SS.us'  (與mysql有區別 '%Y-%m-%d %H:%i:%s' )
  • 時間轉換(當前時間是:2020-06-16 21:59:46.796948+08)

  select extract(epoch from now());       -- 當前時間轉時間戳:1592313871.21819

  select extract(epoch from cast('2020-06-16 12:15:28' as timestamp));    -- 指定時間轉時間戳:1592280928

  select to_timestamp(1592280928);      -- 時間戳轉日期:2020-06-16 12:32:08+08

  select to_char(now(),'yyyy-mm-dd hh:mi:ss.us');    -- 時間格式化(十二小時制):2020-06-16 09:27:46.623385

  select to_char(now(),'yyyy-mm-dd hh24:mi:ss.us');    --時間格式化(二十四小時制):2020-06-16 21:32:31.248214

  select now()::text;              --時間轉字符串:2020-06-16 21:59:46.796948+08

  select substr(to_timestamp(1592313871218.19/1000)::text,0,20);      -- 時間戳轉字符串:2020-06-16 21:24:31

  select current_timestamp;          --獲取時間戳:2020-06-16 21:38:47.23874+08

  select now();                --獲取當前時間:2020-06-16 21:38:47.23874+08

  select current_date;             --獲取當前日期:2020-06-16

  select current_time;             -- 獲取當前時間:21:41:17.270263+08

  select '2020-06-16'::timestamp;        --獲取時間戳:2020-06-16 00:00:00

  select cast('2020-06-16' as timestamp);     --獲取時間戳:2020-06-16 00:00:00

  select timestamp '2020-06-16 18:54:54';    --轉換爲時間戳:2020-06-16 18:54:54

  select to_date('20200616','yyyymmdd');    --轉換字符串:2020-06-16

  select now() + interval '1 day' + interval '3 month' + interval '2 hour';  --日期加一天三個月兩小時:2020-09-17 23:51:24.23632+08

  select date (now());              --獲取當前日期:2020-06-16

  select now()::timestamp +'-2 year';        -- 兩年前的時間:2018-06-16 21:58:20.716811

  select now()::timestamp + '1 year';        -- 當前時間加一年:2021-06-16 21:58:40.158335

  select now()::timestamp + '1 month';        -- 當前時間加一個月:2020-07-16 21:58:51.285984

  select now()::timestamp + '1 day';        -- 當前時間加一天:2020-06-17 21:59:02.155376

  select now()::timestamp + '1 hour';         --當前時間加一個小時:2020-06-16 23:00:30.322946

  select now()::timestamp + '1 min';         --當前時間加一分鐘:2020-06-16 22:01:57.065112

  select now()::timestamp + '1 sec';         --當前時間加一秒鐘:2020-06-16 22:01:13.33365

  select current_date - interval '1 day';        --昨天日期:2020-06-15 00:00:00

  select current_date - interval '1 month';      --上月當日時間:2020-05-16 00:00:00

  select current_date - interval '1 year';        --去年當日時間:2019-06-16 00:00:00

  select date_trunc('months',now())::date;      --當月第一天:2020-06-01

  select date_trunc('months',now())::date - interval '1 year';      --上年本月第一天:2019-06-01 00:00:00

  select date_trunc('months',current_date - interval '1 year')::date;    --去年本月第一天:2019-06-01

  select now()::timestamp + '1 year 1 month 1 day 1 hour 1 min 1 sec';   --加1年1月1天1時1分1秒:2021-07-17 23:06:24.52713

  select now()::timestamp + (col_name || ' day')::interval from table_name;   --把col字段轉換成天 然後相加

  select timestamp without time zone '2020-06-16 22:09:47.078+08';     --去除時區:2020-06-16 22:09:47.078

  select extract(year from now());          --獲取年份:2020

  select extract(month from now());         --獲取月份:6

  select extract(day from now());          --獲取天:16

 

作者:Jason Zeng 於 2020-05-14
博客:https://blog.csdn.net/Z645817
GItHub:https://github.com/lovelifeming
嚴正聲明:
1.由於本博客部分資源來自互聯網,版權均歸原作者所有。轉載的目的是用於學術交流與討論學習,將不對任何資源負法律責任。
2.若無意中侵犯到您的版權利益,請來信聯繫我,我會在收到信息後會儘快給予處理!
3.所有資源內容僅供學習交流之用,請勿用作商業用途,謝謝。
4.如有轉發請註明出處,來源於 https://blog.csdn.net/Z645817,謝謝合作。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章