当前位置:网站首页>常用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 |
边栏推荐
- 喜迎市科协“十大”•致敬科技工作者 | 卢毅:守护电网传输安全的探索者
- 纳人才,谋发展 | 人大金仓喜获“最佳雇主校招案例奖”
- [cloud resident co creation] break through the performance bottleneck of image recognition through rust language computing acceleration technology
- golang7_TCP编程
- How goby exports scan results
- 揭秘百度智能测试在测试自动执行领域实践
- 手把手教你在windows上安装mysql8.0最新版本数据库,保姆级教学
- 文物数字藏品,开启文化传承的新方式
- [graduation season · advanced technology Er] 10.76 million graduates, the most difficult employment season in history? I can't roll it up again. I lie down again and again. Where is the road?
- 灵感收集·创意写作软件评测:Flomo、Obsidian Memo、Napkin、FlowUs
猜你喜欢

Installation and removal of cover for CPU protection on desktop motherboard

Applet Wechat: un nouveau réseau exclusif de microgroupes de développement de Cloud

深度学习的坎坷六十年

Summary of binary tree exercises

goby如何导出扫描结果

Appkey when applying for offline packaging of uniapp

Industry analysis - quick intercom, building intercom

二叉树习题总结

I talked about exception handling for more than half an hour during the interview yesterday

Deecamp2022 officially opened! Likaifu and zhangyaqin personally teach master courses 𞓜 innovation
随机推荐
MySQL数据库:drop、truncate、delete的区别
Source code migration from X86 platform to Kunpeng platform based on Kunpeng development kit [play with Huawei cloud]
留给比亚迪的时间还有三年
Getting started with SQLite3
Investors fell off the altar: 0 sales in half a year, transferred to business and delivered takeout
Wechat applet: repair collection interface version cloud development expression package
php-fpm 启动参数及重要配置详解
leetcode:226. 翻转二叉树
MySQL intercepts the string to remove duplication, and MySQL intercepts the string to remove reassembly
微信小程序:全新獨家雲開發微群人脈
Sixty years of deep learning
文物数字藏品,开启文化传承的新方式
Stable currency risk profile: are usdt and usdc safe?
微信小程序:图片秒加水印制作生成
Korean AI team plagiarizes the shock academic world! One tutor with 51 students, or plagiarism recidivist
[graduation season] it's worth it all the way over the past four years -- advice from the senior students
【烹饪记录】--- 酸辣白菜
STM32 watchdog study
[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)
动荡的中介生意,不安的租房人