PostgreSQL的时间函数使用整理

来源:未知 责任编辑:责任编辑 发表时间:2014-02-02 17:50 点击:
PG的时间函数使用整理如下 
 
1.获取系统时间函数
select now();                --2012-05-12 18:51:59.562+08
select current_timestamp;    --2012-05-12 18:52:12.062+08
select current_date;         --2012-05-12
select current_time;         --18:53:23.234+08
2.时间的计算   www.2cto.com  
--使用interval
select now()+interval '2 day';  --2012-05-14 20:05:32.796+08 2天后
select now()-interval '2 day';  --2012-05-10 20:07:23.265+08 2天前
select now()+interval '2 hour'; --2012-05-12 22:06:38.375+08 2小时后
....
 
interval可以不写,其值可以是
Abbreviation  Meaning
Y             Years
M             Months (in the date part)
W             Weeks
D             Days
H             Hours
M             Minutes (in the time part)
3.时间的截取 
--使用extract extract(interval,timestamp);
select extract(year from now());  --2012
select extract(m from now());     --5 5月份
...  www.2cto.com  
 
interval值参考上面
4.时间的转换
select timestamp '2012-05-12 18:54:54';  --2012-05-12 18:54:54
select date '2012-05-12 18:54:54';       --2012-05-12
select time  '2012-05-12 18:54:54';      --18:54:54
select TIMESTAMP WITH TIME ZONE '2012-05-12 18:54:54'   
--2012-05-12 18:54:54+08
 
--与unix时间戳的转换
SELECT TIMESTAMP 'epoch' + 1341174767 * INTERVAL '1 second'; 
--2012-07-01 20:32:47
以上是基本的PG函数使用,可满足一般的开发运维应用
 
 
 
作者 kenyon
    发表评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    用户名: 验证码:点击我更换图片
    最新评论 更多>>

    推荐热点

    • Request.ServerVariables 参数大全
    • 执行全文索引时出现权限不足的解决方法
    • 导入excel文件处理流程节点的解决方案
    • 查看sql修改痕迹(SQL Change Tracking on Table)
    • App数据层设计及云存储使用指南
    • PostgreSQL启动过程中的那些事三:加载GUC参数
    • MongoDB安装为Windows服务方法与注意事项
    • Percolator与分布式事务思考(二)
    • 写给MongoDB开发者的50条建议Tip1
    网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
    Copyright © 2008-2015 计算机技术学习交流网. 版权所有

    豫ICP备11007008号-1