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,谢谢合作。

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