`
wudixiaotie
  • 浏览: 132000 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

postgresql 函数中日期的加减

阅读更多

在PostgreSQL中可以直接对时间进行加减运算:、

SELECT now()::timestamp + '1 year';  --当前时间加1年
SELECT now()::timestamp + '-1 year';  --当前时间减1年


SELECT now()::timestamp + '1 month';  --当前时间加一个月
SELECT now()::timestamp + '-1 month';  --当前时间减一个月


SELECT now()::timestamp + '1 day';  --当前时间加一天
SELECT now()::timestamp + '-1 day';  --当前时间减一天


SELECT now()::timestamp + '1 hour';  --当前时间加一个小时
SELECT now()::timestamp + '-1 hour';  --当前时间减一个小时


SELECT now()::timestamp + '1 min';  --当前时间加一分钟
SELECT now()::timestamp + '-1 min';  --当前时间减一分钟


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

SELECT now()::timestamp + ('-' || col || ' day')::interval FROM table --把col字段转换成天 然后相减

 

当前时间:current_timestamp

当前日期:current_date

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics