Greenplum数据库,也就是我们常说的gp数据库,其sql语法与Hive、Mysql等有些不同,现将常用的时间操作函数整理如下,以供参考。 获取当前时间、当前日期。 select now();
Greenplum数据库,也就是我们常说的gp数据库,其sql语法与Hive、Mysql等有些不同,现将常用的时间操作函数整理如下,以供参考。
获取当前时间、当前日期。
select now(); -- 当前日期时间>> 2023-02-28 07:45:43.24568+08select current_time; -- 当前时间>> 07:45:43.24568+08select current_date; -- 当前日期>> 2023-02-28select date(now()); -- 当前日期>> 2023-02-28
获取当前时间的后一年、后一个月、后一天等。
select now()::timestamp + '1 year'; -- 当前时间后一年,'-1 year'表示前一年select now()::timestamp + '1 month'; -- 当前时间后一个月select now()::timestamp + '1 day'; -- 当前时间后一天select now()::timestamp + '1 hour'; -- 当前时间后一个小时select now()::timestamp + '1 min'; -- 当前时间后一分钟select now()::timestamp + '1 sec'; -- 当前时间后一秒钟
那年今日。
select current_date - interval '1 day'; -- 昨天日期select current_date - interval '1 month'; -- 上月当日日期select current_date - interval '1 year'; -- 去年当日日期select date_trunc('months',now())::date; -- 当月第一天select now() + interval '3 day' + interval '4 hour'; -- 当前时间后三天四小时
时间格式转换。
select extract(epoch from cast('2023-02-28 07:45:43' as timestamp)); -- 时间转时间戳select to_timestamp(1592280928); -- 时间戳转时间select now()::text; -- 时间转字符串select to_char(now(),'yyyy-mm-dd hh:mi:ss.us'); -- 时间格式化(十二小时制)select to_char(now(),'yyyy-mm-dd hh24:mi:ss.us'); -- 时间格式化(二十四小时制)
去除时区。
select timestamp without time zone '2023-02-28 07:45:43.24568+08'; -- 去除时区
想学习更多的excel、python、SQL等数据分析知识,欢迎关注WX公众号:且听数据说。等着你来。
来源地址:https://blog.csdn.net/weixin_44033063/article/details/129251839
--结束END--
本文标题: SQL | Greenplum数据库常用的时间操作
本文链接: https://www.lsjlt.com/news/416295.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-06-06
2024-06-06
2024-06-05
2024-06-04
2024-06-04
2024-06-03
2024-06-03
2024-06-03
2024-06-04
2024-06-03
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0