PostgreSQL: 浅谈 PostgreSQL 的 timestamp 类型
来源:互联网 发布:ibm区块链数据追溯 编辑:程序博客网 时间:2024/05/18 03:49
转自:francs 的http://francs3.blog.163.com/blog/static/4057672720125723619910/
今天再次有同事问我 PostgreSQL 中时间类型的格式显示问题,例如 " 2012-06-07 14:00:02.412827+08"
如何显示成 " 2012-06-07 14:00:02" ,既然问这个问题的人还不少,于是决定记下来,尽管内容比较简单。在
回答这个问题之前,先来看看 PG 的手册,补充下基础知识。
一 PostgreSQL 中的时间类型如下
备注:这里不准备详细介绍各种类型,请注意上面表格中的[ (p) ] ,这个是什么呢?这个是指时间的精度,
time, timestamp, 和 interval 类型都可以指定精度,精度的取值范围是 0 到 6, 下面通过具体
实验来体验下精度。
二 current_timestamp 实验
--2.1 查询 current_timestamp
now
-------------------------------
2012-06-07 14:00:02.412827+08
(1 row)
备注: current_timestamp 函数返回时间类型为 timestamp with time zone,故返回结果后面包括时区 +08 ,
以及精度 412827,那么如何去掉精度和时区呢?
--2.2 去掉精度
timestamptz
------------------------
2012-06-07 14:07:17+08
(1 row)
--2.3 去掉时区
timestamp
---------------------
2012-06-07 14:07:49
(1 row)
--2.4 也可以用 cast 函数类型转换
timestamp
---------------------
2012-06-07 14:14:55
(1 row)
--2.5 了解 [p] 的含义 skytf=> select current_timestamp(2)::timestamp without time zone;
timestamp
------------------------
2012-06-07 14:15:42.64
(1 row)
skytf=> select current_timestamp(6)::timestamp without time zone;
timestamp
----------------------------
2012-06-07 14:15:46.281422
(1 row)
备注:可见 [p] 是指时间类型小数点后面的精度,如果 p 指定 2,则精度为2,如果 p 指定 6
则精度为 6; 所以在定义表的时候就应该事先定义 timestamp 时间类型的精度。
三 创建表测试,定义时间类型精度为0
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "test_p_pkey" for table "test_p"
CREATE TABLE
skytf=> \d test_p
Table "skytf.test_p"
Column | Type | Modifiers
-------------+--------------------------------+-----------
id | integer | not null
create_time | timestamp(0) without time zone |
Indexes:
"test_p_pkey" PRIMARY KEY, btree (id)
skytf=> select current_timestamp;
now
-------------------------------
2012-06-07 14:18:31.683105+08
(1 row)
skytf=> insert into test_p values (1,current_timestamp);
INSERT 0 1
skytf=> select * from test_p;
id | create_time
----+---------------------
1 | 2012-06-07 14:19:02
(1 row)
- PostgreSQL: 浅谈 PostgreSQL 的 timestamp 类型
- 浅谈 PostgreSQL 的 timestamp 类型
- 浅谈 PostgreSQL 的 timestamp 类型
- timestamp[] postgresql
- postgresql timestamp的精度问题
- PostgreSQL的日志类型
- PostgreSQL的日志类型
- PostgreSQL的日志类型
- PostgreSQL的日志类型
- PostgreSQL的日志类型
- PostgreSQL中timestamp相关的SQL语句
- PostgreSql中的timestamp用法
- PostgreSQL 枚举类型的使用
- postgreSQL ARRAY 类型的使用
- postgresql timestamp timestamptz 使用注意事项
- postgreSQL
- PostgreSQL
- PostgreSQL
- 网络游戏服务器开发(一)
- Apache Ambari
- SVN中文手册
- 归并排序的一点见解
- jQuery 选择器
- PostgreSQL: 浅谈 PostgreSQL 的 timestamp 类型
- java 枚举类型enum 的使用
- Linux 3.10 ARM Device Tree 的初始化
- 中英文url解码vc++源程序
- SQL2005四个排名函数(row_number、rank、dense_rank和ntile)的比较
- 小马哥---高仿苹果6s出现市场,真假难辨 警惕中招
- Github的一个免费编程书籍列表
- win32 剪切板操作 (转)
- 英特尔连续亏损,内部需调整