【PostgreSQL-9.6.3】函数(3)--日期和时间函数
来源:互联网 发布:商场数据分析 编辑:程序博客网 时间:2024/06/05 09:31
在PostgreSQL中,DATE、TIME、TIMESTAMP是三种不同的数据类型。DATE表示日期类型,格式为YYYY-MM-DD或YYYYMMDD;TIME表示时间类型,格式为hh:mi:ss;
TIMESTAM类型的格式一般为'YYYY-MM-DD hh:mi:ss'。
1. current_date、current_time和localtime
current_date按照YYYY-MM-DD格式返回当前日期;current_time获取系统的当前时间;localtime的作用和current_time相同,不同点在于localtime返回的时间不带时区。
test=# select current_date,current_time,localtime; date | timetz | time ------------+--------------------+----------------- 2017-05-31 | 21:26:47.731091+08 | 21:26:47.731091(1 row)
2. current_timestamp、localtimestamp和now()
这三个函数的作用都是返回系统当前的日期和时间。
test=# select current_timestamp,localtimestamp,now(); now | timestamp | now ------------------------------+---------------------------+------------------------------ 2017-05-31 21:28:24.17628+08 | 2017-05-31 21:28:24.17628 | 2017-05-31 21:28:24.17628+08(1 row)
3. extract(type from date)
extract函数从日期中提取部分值。
(1)依次提取日期中的年份、月份、日部分
test=# select extract(year from timestamp '2017-05-31 21:31:20') as year,extract(month from timestamp '2017-05-31 21:31:20') as month,extract(day from timestamp '2017-05-31 21:31:20') as day; year | month | day ------+-------+----- 2017 | 5 | 31(1 row)
(2)依次查询指定日期是所在年的第几天、所在周的星期几、所在年的第几季度
test=# select extract (doy from timestamp '2017-05-31 21:31:20') as doy,extract (dow from timestamp '2017-05-31 21:31:20') as dow,extract(quarter from timestamp '2017-05-31 21:31:20') as quarter; doy | dow | quarter -----+-----+--------- 151 | 3 | 2(1 row)
4. 日期的运算
test=# select date '2017-05-31' + integer '10'; ?column? ------------ 2017-06-10(1 row)test=# select date '2017-05-31' + interval '5 hour'; ?column? --------------------- 2017-05-31 05:00:00(1 row)test=# select date '2017-05-31' + time '12:00'; ?column? --------------------- 2017-05-31 12:00:00(1 row)test=# select timestamp '2017-05-31 21:31:20' + interval '3 hour'; ?column? --------------------- 2017-06-01 00:31:20(1 row)test=# select date '2017-07-22' - date '2017-05-31'; ?column? ---------- 52(1 row)test=# select date '2017-05-31' - integer '10'; ?column? ------------ 2017-05-21(1 row)test=# select 15 * interval '2 day'; ?column? ---------- 30 days(1 row)test=# select 50 * interval '2 second'; ?column? ---------- 00:01:40(1 row)test=# select interval '1 hour' / integer '2'; ?column? ---------- 00:30:00(1 row)
阅读全文
0 0
- 【PostgreSQL-9.6.3】函数(3)--日期和时间函数
- PostgreSQL的时间/日期函数使用
- PostgreSQL的时间/日期函数使用
- PostgreSQL的时间/日期函数使用
- PostgreSQL的时间/日期函数使用
- 函数整理(日期和时间函数)
- 日期和时间函数
- 时间和日期函数
- 日期和时间函数
- 时间和日期函数
- 函数:日期和时间函数
- mysql系统函数3----日期时间函数
- SQL 10 函数 3 日期时间函数 - 1 日期、时间、日期时间与时间戳
- Mysql函数总结(3)——日期时间函数
- PostgreSQL日期函数备忘
- PostgreSQL日期函数
- postgreSQL 日期函数 Extract
- 日期 和 时间 判断(函数 事例)
- 测试05-02
- cf 106d Treasure Island
- 巩朋:我的算法学习之路
- Keil SIGABRT Abnormal termination
- spring的AOP(四)----Spring AOP 编程(Advisor)
- 【PostgreSQL-9.6.3】函数(3)--日期和时间函数
- 《挑战程序设计竞赛》阅读笔记一 之 ALDS1_1_C Prime Numbers
- 学习淘淘商城第八十一课(SSO 系统分析)
- RecyclerView.ItemDecoration的使用
- python——堡垒机
- 5D Visualization and Annotation CATMAID Documentation
- 理解激活函数在神经网络模型构建中的作用
- RTP_h264打包源码
- find算法函数查找示例