浅谈 PostgreSQL 的 timestamp 类型

news/2024/7/9 22:36:19 标签: postgresql, timestamp
转载自:http://blog.csdn.net/menghuannvxia/article/details/77883743


一 PostgreSQL 中的时间类型如下
Name                                          Storage Size   Description                                 Low Value    High Value     Resolution
timestamp [ (p) ] [ without time zone ] 8 bytes   both date and time (no time zone)  4713 BC 294276 AD    1 microsecond / 14 digits
timestamp [ (p) ]  with time zone         8 bytes   both date and time, with time zone  4713 BC 294276 AD    1 microsecond / 14 digits
date                                                4 bytes   date (no time of day)                        4713 BC 5874897 AD      1 day
time [ (p) ][ without time zone ]            8 bytes   time of day (no date)                        00:00:0024:00:00     1 microsecond / 14 digits
time [ (p) ]with time zone           12 bytes   times of day only, with time zone  00:00:00+1459 24:00:00-1459    1 microsecond / 14 digits
interval [ fields ] [ (p) ]                           12 bytes   time interval                                 -178000000 years178000000 years    1 microsecond / 14 digits
 

  备注:这里不准备详细介绍各种类型,请注意上面表格中的[ (p) ] ,这个是什么呢?这个是指时间的精度,
  time, timestamp, 和 interval 类型都可以指定精度,精度的取值范围是 0 到 6, 下面通过具体实验来体验下精度。


二 current_timestamp 实验
--2.1 查询  current_timestamp


 skytf=> select current_timestamp;
              now              
-------------------------------
 2012-06-07 14:00:02.412827+08
(1 row)        
   
 备注: current_timestamp 函数返回时间类型为 timestamp with  time zone,故返回结果后面包括时区 +08 ,以及精度 412827,那么如何去掉精度和时区呢?
 


--2.2 去掉精度


 skytf=> select current_timestamp(0);
      timestamptz       
------------------------
 2012-06-07 14:07:17+08
(1 row)
   


--2.3 去掉时区


 skytf=> select current_timestamp(0)::timestamp without time zone;
      timestamp      
---------------------
 2012-06-07 14:07:49
(1 row)
   
select to_timestamp('2015/9/18 13:57:05','yyyy/mm/dd hh24:mi:ss')::timestamp without time zone;
--2015-09-18 13:57:05




--2.4 也可以用 cast 函数类型转换


 skytf=> select cast (current_timestamp(0) as  timestamp without time zone);
      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


 skytf=> create table test_p (id int4 primary key, create_time  timestamp(0) without time zone);
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)                    

http://www.niftyadmin.cn/n/1598905.html

相关文章

PostgreSQL的存储过程简单入门

转载自:http://blog.sina.com.cn/s/blog_6137d9b70102v5qd.html PostgreSQL 存储过程定义格式如下:■结构 PL/pgSQL是一种块结构的语言,比较方便的是用pgAdmin III新建Function,填入一些参数就可以了。基本上是这样的&#xff1a…

Linux-0.11内核编译问题

弄了好长时间,只是可以用linux-0.11我的Linux实验室环境,使用bochs模拟,进入Linux-0.11 cd .. cd src/linux make 这样就编译成Image了 mkdir a: mkdir a:/boot/ mcopy Image a:/boot/ 将镜像文件拷贝到启动软盘 修改menu.lst 最后…

postgresql 数据库时间间隔数据查询

当前时间向前推一天:SELECT current_timestamp - interval 1 day当前时间向前推一个月:SELECT current_timestamp - interval 1 month当前时间向前推一年:SELECT current_timestamp - interval 1 year当前时间向前推一小时:SELECT…

ext修改行颜色,隔行颜色没变解决方案

在ext修改行颜色的过程中,发现奇数行的换颜色了,偶数行没换颜色,后来查文档,发下: stripeRows: Boolean隔行换色 True实现隔行换颜色的效果。(默认为true) 通过把样式x-grid-row-alt交替添加到g…

设计模式总结:一抽象,二法则,三特点,四原则 ,五关系

设计模式第二遍的学习.现在对设计模式做一个系统的总结. 设计模式学习的四个层次: 第一层,理解各个模式.知道各个模式是做什么用的. 第二层,对各个设计模式的代码实现. 第三层.知道在什么情况用哪个模式. 第四层.自己根据自己遇到的问题来设计模式. 经过这一个月的设计模式学习…

Wine 编译安装

UBUNTU10.10系统用apt-get安装后参考网上的做法但是中文还是乱码,于是自己编译 gcc version 4.4.5 (Ubuntu/Linaro 4.4.4-14ubuntu5) 编译过程中出现问题 term.c:33: fatal error: term.h: No such file or directory 由于缺少库 sudo apt-get install libncur…

2021-07-22 markdown学习笔记

markdown学习笔记 Markdown标题 使用 # 号可表示 1-6 级标题,一级标题对应一个 # 号,二级标题对应两个 # 号,以此类推 和 - 标记 例: # 一级标题 ## 二级标题一级标题 二级标题 -------效果: 一级标题 二级标题 …

Oracle中生成随机数方法

--生成1到2之间的随机数 select dbms_random.value(1,2) from dual; 一、dbms_random.value函数 dbms_random是一个可以生成随机数值或者字符串的程序包。这个包有initialize()、seed()、terminate()、value()、normal()、random()、string()等几个函数,但value()…