
Postgresql 日期和时间类型
发布日期:2021-05-08 12:23:55
浏览次数:10
分类:原创文章
本文共 1783 字,大约阅读时间需要 5 分钟。
作者:瀚高PG实验室 (Highgo PG Lab)- 天蝎座
timestamp with[out] time zone 日期时间(时区) time with[out] time zone 只用于一日内的时间(时区) 可以精确到毫秒。MySQL只能到秒。默认都不会显示时区,需要显示的指定with time zone。highgo=# select time '120900'; time ---------- 12:09:00(1 row)highgo=# select time '120900 PST'; time ---------- 12:09:00(1 row)highgo=# select time WITH time zone '120900 PST'; timetz ------------- 12:09:00-08(1 row)在输入日期时,有很多种格式。有时候需要确定datestyle。highgo=# create table t(date date);CREATE TABLEhighgo=# insert into t values (date '12-18-2010');INSERT 0 1highgo=# select * from t; date ------------ 2010-12-18(1 row)highgo=# show datestyle; DateStyle ----------- ISO, MDY(1 row)highgo=# set datestyle = 'YMD';SEThighgo=# insert into t values (date '2010-12-19');INSERT 0 1highgo=# select * from t; date ------------ 2010-12-18 2010-12-19(2 rows)注: 建议使用 2017-12-18 这种方式输入日期。任何方式下都是2017年12月18日。PostgreSQL 提供可许多返回当前日期和时间的函数。部分函数按照当前事务的开始时刻返回结果:CURRENT_DATECURRENT_TIMECURRENT_TIMESTAMPCURRENT_TIME(precision)LOCALTIMELOCALTIMESTAMPnow()transaction_timestamp() 感觉这个描述更符合实际情况另外部分函数返回实时时间值,在事务中也会随时间变化。statement_timestamp()clock_timestamp()timeofday() 返回的是text字符串在同一个事务中,和now()同类型的函数不会变化,和statement_timestamp()同类型的函数则相反。highgo=# begin;BEGINhighgo=# select now(); now ------------------------------- 2017-12-18 12:18:32.263891+08(1 row)highgo=# select now(); now ------------------------------- 2017-12-18 12:18:32.263891+08(1 row)highgo=# select statement_timestamp(); statement_timestamp ------------------------------- 2017-12-18 12:19:15.463267+08(1 row)highgo=# select statement_timestamp(); statement_timestamp ------------------------------- 2017-12-18 12:19:18.975693+08(1 row)highgo=# commit;COMMIT
发表评论
最新留言
关注你微信了!
[***.104.42.241]2025年03月30日 00时26分02秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Processes, threads and goroutines
2019-03-05
c++中的10种常见继承
2019-03-05
E28 LoRa模块透传 定点传输 RSSI测试与MicroPython应用
2019-03-05
Vue学习—深入剖析渲染函数
2019-03-05
Vue学习—深入剖析函数式组件
2019-03-05
简单Makefile的编写
2019-03-05
使用BAT批处理 匹配查找指定文件夹,并在当文件夹下创建空文件
2019-03-05
wxpython的Hello,World代码探索
2019-03-05
【数字图像处理】OpenCV3 学习笔记
2019-03-05
【单片机开发】智能小车工程(经验总结)
2019-03-05
【单片机开发】基于stm32的掌上游戏机设计 (项目规划)
2019-03-05
KeepAlived介绍、配置示例、KeepAlived配置IPVS、调用脚本进行监控
2019-03-05
【Numpy学习】np.count_nonzero()用法解析
2019-03-05
Scala集合-数组、元组
2019-03-05
Flink Standalone集群安装和部署
2019-03-05
JAVA网络爬虫01-http client爬取网络内容
2019-03-05
04 程序流程控制
2019-03-05
java并发编程(1)
2019-03-05
C++&&STL
2019-03-05