在PostgreSQL中使用timestamp数据类型

前端之家收集整理的这篇文章主要介绍了在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)
那么

@H_502_35@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成功了,见下:

@H_502_35@postgres=# \d abc_s.test_lei 数据表 "abc_s.test_lei" 栏位 | 类型 | Collation | Nullable | Default ------+--------------------------------+-----------+----------+--------- c1 | timestamp(6) without time zone | | | ---->>>>可以看到,自动给变成6了。因为最大到6--微秒 postgres=#
第二点:
sql语句中想插入400毫秒,就要写:
@H_502_35@insert into test_t1 values(to_timestamp('2049-11-22 21:21:21.4','YYYY-MM-DD HH24:MI:SS.MS'));
sql语句中想插入40毫秒,就要写:
@H_502_35@insert into test_t1 values(to_timestamp('2049-11-22 21:21:21.04','YYYY-MM-DD HH24:MI:SS.MS'));
sql语句中想插入4毫秒,就要写:
@H_502_35@insert into test_t1 values(to_timestamp('2049-11-22 21:21:21.004','YYYY-MM-DD HH24:MI:SS.MS')); 因为1000毫秒=1秒

猜你在找的Postgre SQL相关文章