当前位置:网站首页>常用postgresql数据操作备忘:时间
常用postgresql数据操作备忘:时间
2022-06-29 13:30:00 【自己的九又四分之三站台】
1. 当前时间
select now();
select CURRENT_DATE;
select CURRENT_TIME;
select CURRENT_TIMESTAMP;
select CURRENT_TIME(precision);
select CURRENT_TIMESTAMP(precision);
select LOCALTIME;
select LOCALTIMESTAMP;
select LOCALTIME(precision);
select LOCALTIMESTAMP(precision);
--示例
SELECT CURRENT_TIME;
结果:14:39:53.662522-05
SELECT CURRENT_DATE;
结果:2001-12-23
SELECT CURRENT_TIMESTAMP;
结果:2001-12-23 14:39:53.662522-05
SELECT CURRENT_TIMESTAMP(2);
结果:2001-12-23 14:39:53.66-05
SELECT LOCALTIMESTAMP;
结果:2001-12-23 14:39:53.662522
1.1. 时间函数
| 函数 | 返回类型 | 描述 | 例子 | 结果 |
|---|---|---|---|---|
| age(timestamp, timestamp) | interval | 减去参数,生成一个使用年、月(而不是只用日)的“符号化”的结果 | age(timestamp ‘2001-04-10’, timestamp ‘1957-06-13’) | 43 年 9 月 27 日 |
| age(timestamp) | interval | 从current_date(在午夜)减去 | age(timestamp ‘1957-06-13’) | 43 years 8 mons 3 days |
| clock_timestamp() | timestamp with time zone | 当前日期和时间(在语句执行期间变化);见第 9.9.4 节 | ||
| current_date | date | 当前日期;见第 9.9.4 节 | ||
| current_time | time with time zone | 当前时间(一天中的时间);见第 9.9.4 节 | ||
| current_timestamp | timestamp with time zone | 当前日期和时间(当前事务开始时);见第 9.9.4 节 | ||
| date_part(text, timestamp) | double precision | 获得子域(等价于extract);见第 9.9.1 节 | date_part(‘hour’, timestamp ‘2001-02-16 20:38:40’) | 20 |
| date_part(text, interval) | double precision | 获得子域(等价于extract);见第 9.9.1 节 | date_part(‘month’, interval ‘2 years 3 months’) | 3 |
| date_trunc(text, timestamp) | timestamp | 截断到指定精度;另见第 9.9.2 节 | date_trunc(‘hour’, timestamp ‘2001-02-16 20:38:40’) | 2001-02-16 20:00:00 |
| date_trunc(text, interval) | interval | 截断到指定精度;另见第 9.9.2 节 | date_trunc(‘hour’, interval ‘2 days 3 hours 40 minutes’) | 2 days 03:00:00 |
| extract(field from timestamp) double precision | 获得子域;见第 9.9.1 节 | extract(hour from timestamp ‘2001-02-16 20:38:40’) | 20 | |
| extract(field from interval) | double precision | 获得子域;见第 9.9.1 节 | extract(month from interval ‘2 years 3 months’) | 3 |
| isfinite(date) | boolean | 测试有限日期(不是+/-无限) | isfinite(date ‘2001-02-16’) | true |
| isfinite(timestamp) | boolean | 测试有限时间戳(不是+/-无限) | isfinite(timestamp ‘2001-02-16 21:28:30’) | true |
| isfinite(interval) | boolean | 测试有限间隔 | isfinite(interval ‘4 hours’) | true |
| justify_days(interval) | interval | 调整间隔这样30天时间周期可以表示为月 | justify_days(interval ‘35 days’) | 1 mon 5 days |
| justify_hours(interval) | interval | 调整间隔这样24小时时间周期可以表示为日 | justify_hours(interval ‘27 hours’) | 1 day 03:00:00 |
| justify_interval(interval) | interval | 使用justify_days和justify_hours调整间隔,使用额外的符号调整 | justify_interval(interval ‘1 mon -1 hour’) | 29 days 23:00:00 |
| localtime | time | 当前时间(一天中的时间);见第 9.9.4 节 | ||
| localtimestamp | timestamp | 当前日期和时间(当前事务的开始);见第 9.9.4 节 | ||
| make_date(year int, month int, day int) | date | 从年、月、日域创建日期 | make_date(2013, 7, 15) | 2013-07-15 |
| make_interval(years int DEFAULT 0, months int DEFAULT 0, weeks int DEFAULT 0, days int DEFAULT 0, hours int DEFAULT 0, mins int DEFAULT 0, secs double precision DEFAULT 0.0) | interval | 从年、月、周、日、时、分、秒域创建 | interval make_interval(days => 10) | 10 days |
| make_time(hour int, min int, sec double precision) | time | 从时、分、秒域创建时间 | make_time(8, 15, 23.5) | 08:15:23.5 |
| make_timestamp(year int, month int, day int, hour int, min int, sec double precision) | timestamp | 从年、月、日、时、分、秒域创建时间戳 | make_timestamp(2013, 7, 15, 8, 15, 23.5) | 2013-07-15 08:15:23.5 |
| make_timestamptz(year int, month int, day int, hour int, min int, sec double precision, [ timezone text ]) | timestamp with time zone | 从年、月、日、时、分、秒域创建带时区的时间戳。如果没有指定timezone, 则使用当前时区。 | make_timestamptz(2013, 7, 15, 8, 15, 23.5) | 2013-07-15 08:15:23.5+01 |
| now() | timestamp with time zone | 当前日期和时间(当前事务的开始);见第 9.9.4 节 | ||
| statement_timestamp() | timestamp with time zone | 当前日期和时间(当前事务的开始);见第 9.9.4 节 | ||
| timeofday() | text | 当前日期和时间(像clock_timestamp,但是作为一个text字符串);见第 9.9.4 节 | ||
| transaction_timestamp() | timestamp with time zone | 当前日期和时间(当前事务的开始);见第 9.9.4 节 | ||
| to_timestamp(double precision) | timestamp with time zone | 把 Unix 时间(从 1970-01-01 00:00:00+00 开始的秒)转换成 timestamp | to_timestamp(1284352323) | 2010-09-13 04:32:03+00 |
边栏推荐
- 每周 Postgres 世界动态 2022w25
- How to install MySQL 8.0 on rocky Linux and almalinux
- golang7_TCP编程
- 超 Nice 的表格响应式布局小技巧
- grep 精确匹配
- Follow me study hcie big data mining Chapter 1 Introduction to data mining module 1
- 硬件开发笔记(八): 硬件开发基本流程,制作一个USB转RS232的模块(七):创建基础DIP元器件(晶振)封装并关联原理图元器件
- 吐血整理:一份不可多得的架构师图谱!
- 人不成熟的特征
- unity吃豆人小游戏,迷宫实现
猜你喜欢
![[dark horse morning post] the market value of China public education has evaporated by more than 200billion; New Oriental has more than 20million live fans; HM closes its first store in China; Vanke Y](/img/d7/4671b5a74317a8f87ffd36be2b34e1.jpg)
[dark horse morning post] the market value of China public education has evaporated by more than 200billion; New Oriental has more than 20million live fans; HM closes its first store in China; Vanke Y

Online text filter less than specified length tool

微信小程序:图片秒加水印制作生成
![[document translation] camouflaged object detection](/img/30/73a927c05173a95cc5a5d51e182e3b.png)
[document translation] camouflaged object detection

Sixty years of deep learning

丢弃 Tkinter!简单配置快速生成超酷炫 GUI!

Goby full port scan

喜迎市科协“十大”•致敬科技工作者 | 卢毅:守护电网传输安全的探索者

##Mondo Rescue制作镜像文件(有利于镜像损坏恢复)

Tiktok's global short video dominance may be reversed by YouTube
随机推荐
Cloud native (31) | kubernetes chapter kubernetes platform basic pre installed resources
靠代理,靠买断,国产端游的蛮荒时代等待下一个《永劫无间》
Go unit testing introductory practice
win11怎么看cpu几核几线程? win11查看cpu是几核几线程的教程
Turbulent intermediary business, restless renters
Wechat applet: new and exclusive cloud development wechat group contacts
MySQL数据库:drop、truncate、delete的区别
“死掉”的诺基亚,一年躺赚1500亿
zabbix 5.0如何将esxi6.7添加到监控
【黑马早报】中公教育市值蒸发逾2000亿;新东方直播粉丝破2000万;HM关闭中国首店;万科郁亮称房地产已触底;微信上线“大爆炸”功能...
Appkey when applying for offline packaging of uniapp
【VEUX开发者工具的使用-getters使用】
Grep exact match
urllib urllib2
内网穿透(nc)
Applet Wechat: un nouveau réseau exclusif de microgroupes de développement de Cloud
二叉树习题总结
php-fpm 启动参数及重要配置详解
如何优雅的写 Controller 层代码?
Wechat applet: Halloween avatar box generation tool