PostgreSQL数据类型-时间数据类型

PostgreSQL数据类型-时间数据类型,第1张

概述PostgreSQL数据类型支持6种时间数据类型。如下图所示: time、timestamp和interval接受可选精度值 p,精度值是秒域中小数点后保留位数。缺省情况下,精度没有明确限制,timestamp和interval类型p允许范围从 0 到 6。 timestamp默认以8字节整数储存,整个数值范围内保持微秒以上精度,当被修改为双精度浮点数时,极端情况下,精度会下降到毫秒级以上。 in

Postgresql数据类型支持6种时间数据类型。如下图所示:

time、timestamp和interval接受可选精度值 p,精度值是秒域中小数点后保留位数。缺省情况下,精度没有明确限制,timestamp和interval类型p允许范围从 0 到 6。

timestamp默认以8字节整数储存,整个数值范围内保持微秒以上精度,当被修改为双精度浮点数时,极端情况下,精度会下降到毫秒级以上。

interval类型附加选项fIElds通过下列短语限制存储fIElds集合:

YEARMONTHDAYHOURMINUTESECONDYEAR TO MONTHDAY TO HOURDAY TO MINUTEDAY TO SECONDHOUR TO MINUTEHOUR TO SECONDMINUTE TO SECOND

注意如果fIElds和p被指定,fIElds必须包括SECOND,因为精度只应用于秒。

类型time with time zone是sql标准,但可能影响可用性。大多数情况下, date、time、timestamp without time zone和timestamp with time zone组合就能提供任何应用所需全范围日期/时间功能。

时间输入格式为

type[ fIElds ] [ ( 精度 ) ] [ | with time zone] 'timevalue'

如下代码演示插入时间或日期。

test=# create @R_301_5991@ testdatetime(ID int,testdate date,testtime time);CREATE @R_301_5991@test=# insert into testdatetime values(1,'1644-1-1','01:11:11'),(2,'2017-11-9','15:02:22+08');INSERT 0 2test=#

还可以查看数据库系统时间显示格式。例如YMD为年月日格式。

---查看DateStyletest=# show datestyle; DateStyle----------- ISO,YMD(1 行记录)---修改DateStyletest=# set DateStyle='YMD';SETtest=#

插入数值必须符合设定日期格式,否则可能会导致出错或输入错误。

test=# insert into testdatetime values(1,'1795/1/3','01:11:11');                INSERT 0 1test=# insert into testdatetime values(1,'1979 June 1','01:11:11');INSERT 0 1test=# insert into testdatetime values(1,'01:11:11');

如上代码可知,不同DateStyle可能会输出结果不相同,或者提示错误。

推荐"-"作为分隔符,推荐年月日方式输入日期数值。"/"可能会导致歧义。也可以使用"2017-July-1"类似明确无歧义格式输入日期和时间。

---输入公元前时间、儒略历日期---AD 放在最后test=# insert into testdatetime values(3,'117-1-9 BC','16:00:00'),(5,'J2455000','10:17:20');INSERT 0 3test=#

查询已输入数据结果。

test=# select * from testdatetime;                                               ID |   testdate    | testtime----+---------------+----------  1 | 1644-01-01    | 01:11:11  2 | 2017-11-09    | 15:02:22  1 | 1795-01-03    | 01:11:11  1 | 1979-06-01    | 01:11:11  1 | 1979-06-01    | 01:11:11  3 | 0117-01-09 BC | 16:00:00  5 | 2009-06-17    | 10:17:20(7 行记录)test=#

time数据类型等效于time without time zone。当输入带市区格式时,时区信息自动被剔除。

test=# insert into testdatetime values(4,'01:11:11+8');test=# select * from testdatetime where ID =4; ID |  testdate  | testtime----+------------+----------  4 | 1979-06-01 | 01:11:11(1 行记录)test=#---不推荐,无时分秒分隔符,分隔符推荐:test=# insert into testdatetime values(4,'011111+8');INSERT 0 1test=#

输入有时区时间数据使用有时区数据类型。

test=# create @R_301_5991@ testdatetimewithtimezone(ID int,timewithzone time with time zone);CREATE @R_301_5991@---插入不同类型带时区时间数据test=# insert into testdatetimewithtimezone values(1,'09:17:22.011111'),'17:33:59+11'),(3,'231545+07:45');INSERT 0 3test=#

推荐例如"07:59:59.111111+08:00"格式输入数据,时区缩写和声明时区等格式可能导致输入错误发生。

test=# select * from testdatetimewithtimezone; ID |    timewithzone----+--------------------  1 | 09:17:22.011111+08  2 | 17:33:59+11  3 | 23:15:45+07:45(3 行记录)test=#

参考链接:

https://www.postgresql.org/docs/current/static/datatype-datetime.html

总结

以上是内存溢出为你收集整理的PostgreSQL数据类型-时间数据类型全部内容,希望文章能够帮你解决PostgreSQL数据类型-时间数据类型所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/1171904.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存