greenplum 日期及时间函数及时间的加减

来源:互联网 发布:布鲁斯 知乎 编辑:程序博客网 时间:2024/05/16 09:32

1.日期和时间间字段
date:日期字段,格式:2016-04-15
timestamp:时间字段,格式:2016-04-15 20:00:00
2.获取当时时间函数
select now();
select current_timestamp;
select CURRENT_TIME;
select LOCALTIME;
select LOCALTIMESTAMP;
3.获取当天日期
select current_date;


4.日期计算:

--使用interval
当前时间加2天
 select now()+interval '2 day';
当前时间减2天
 select now()-interval '2 day';
当前时间减1个月
 select now()-interval '-2 mon';
当前时间减2小时
 select now()+interval '2 hour';
 
5.时间截取

显示年
select extract(year from now()); 
显示月
select extract(mon from now()); 


6.时间转换
显示年月日时分秒
select timestamp '2012-05-12 18:54:54';
显示年月日
select date '2012-05-12 18:54:54'; 
显示年月日时分秒+毫秒
select TIMESTAMP WITH TIME ZONE '2012-05-12 18:54:54' ;                                                                                        


7.与unix时间戳的转换 
SELECT TIMESTAMP 'epoch' + 1341174767 * INTERVAL '1 second';