当前位置:网站首页>常用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 |
边栏推荐
- Hardware development notes (VIII): basic process of hardware development, making a USB to RS232 module (VII): creating a basic dip component (crystal oscillator) package and associating the principle
- urllib urllib2
- Problems in replacing RESNET convolution of mmdet with ghost convolution group
- 微信小程序:全新独家云开发微群人脉
- Cicd introduction [easy to understand]
- 微信小程序:万圣节头像框生成工具
- vmware虚拟机的作用
- 【毕业季·进击的技术er】1076万毕业生,史上最难就业季?卷又卷不过,躺又躺不平,敢问路在何方?
- [high concurrency] 28000 words' summary of callable and future interview knowledge points. After reading it, I went directly to ByteDance. Forgive me for being a little drifting (middle)
- Teach you how to install the latest version of mysql8.0 database on windows, nanny level teaching
猜你喜欢

Korean AI team plagiarizes the shock academic world! One tutor with 51 students, or plagiarism recidivist

昨天面试居然聊了半个多小时的异常处理

Wechat applet: repair collection interface version cloud development expression package

“死掉”的诺基亚,一年躺赚1500亿

现场快递柜状态采集与控制系统

Equivalence class partition method for test case design method

纳人才,谋发展 | 人大金仓喜获“最佳雇主校招案例奖”

Mondo rescue creates an image file (which is conducive to image damage recovery)

"Dead" Nokia makes 150billion a year
![[document translation] camouflaged object detection](/img/30/73a927c05173a95cc5a5d51e182e3b.png)
[document translation] camouflaged object detection
随机推荐
Wechat applet: new and exclusive cloud development wechat group contacts
Why does ETL often become ELT or even let?
超 Nice 的表格响应式布局小技巧
How to install MySQL 8.0 on rocky Linux and almalinux
Navicat连接MySQL8.0的正确方法(亲测有效)
Basic type variable declaration
HTAP X 云原生: TiDB 加速释放数据价值,实现数据敏捷
Want to make a wechat game for answering questions? Reading this article is enough
揭秘百度智能测试在测试自动执行领域实践
Go unit testing introductory practice
文物数字藏品,开启文化传承的新方式
如何优雅的写 Controller 层代码?
C keyboard hook
Valueerror: only TF native optimizers are supported in Eagle mode
PHP FPM startup parameters and important configuration details
高校女生穿旗袍答辩!网友:导师说论文要是和旗袍一样漂亮就好了
Deecamp2022 officially opened! Likaifu and zhangyaqin personally teach master courses 𞓜 innovation
Investors fell off the altar: 0 sales in half a year, transferred to business and delivered takeout
[high concurrency] cache idea
瑞达期货可以开户吗?安全可靠吗?