PostgreSQL日期类型操作

timestamp关键字

 1.比较大小

select count(*) from tablename where adddate>timestamp '2016-11-14 00:00:00' and adddate < timestamp '2016-11-14 23:59:59'

2.时间加法运算

SELECT now()::timestamp(0) + '1 year';  --当前时间加1年
SELECT now()::timestamp(0) + '1 month';  --当前时间加一个月
SELECT now()::timestamp(0) + '1 day';  --当前时间加一天
SELECT now()::timestamp(0) + '1 hour';  --当前时间加一个小时
SELECT now()::timestamp(0) + '1 min';  --当前时间加一分钟
SELECT now()::timestamp(0) + '1 sec';  --加一秒钟
select now()::timestamp(0) + '1 year 1 month 1 day 1 hour 1 min 1 sec';  --加1年1月1天1时1分1秒
SELECT now()::timestamp(0) + (col || ' day')::interval FROM table --把col字段转换成天 然后相加

3.时间减法运算 

select CURRENT_TIMESTAMP::TIMESTAMP + '-5 day'

参考 Postgresql 时间加减运算 

posted on 2022-04-07 15:28  dreamstar  阅读(608)  评论(0编辑  收藏  举报