在PostgreSQL中使用timestamp数据类型


参考自:https://www.postgresql.org/docs/10/static/functions-formatting.html

  • To get three milliseconds, one must write 12.003, which the conversion treats as 12 + 0.003 = 12.003 seconds.


第一点:众所周知,timestamp数据类型最细粒度到微秒(us),也就是timestamp(6)
那么

postgres=# create table abc_s.test_lei (c1 timestamp(7));
WARNING:  TIMESTAMP(7) precision reduced to maximum allowed, 6
第1行create table abc_s.test_lei (c1 timestamp(7));
                                     ^
WARNING:  TIMESTAMP(7) precision reduced to maximum allowed, 6
CREATE TABLE
postgres=# 

那么如上的语句虽然报错了,但是这个表还是create成功了,见下:

postgres=# \d abc_s.test_lei
                        数据表 "abc_s.test_lei"
 栏位 |              类型              | Collation | Nullable | Default
------+--------------------------------+-----------+----------+---------
 c1   | timestamp(6) without time zone |           |          |


---->>>>可以看到,自动给变成6了。因为最大到6--微秒
postgres=#

第二点: 
sql语句中想插入400毫秒,就要写:
insert into test_t1 values(to_timestamp('2049-11-22 21:21:21.4','YYYY-MM-DD HH24:MI:SS.MS'));

sql语句中想插入40毫秒,就要写:
insert into test_t1 values(to_timestamp('2049-11-22 21:21:21.04','YYYY-MM-DD HH24:MI:SS.MS'));

sql语句中想插入4毫秒,就要写:
insert into test_t1 values(to_timestamp('2049-11-22 21:21:21.004','YYYY-MM-DD HH24:MI:SS.MS'));

因为1000毫秒=1秒

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值