pg 对时间的处理还是很灵活的, + - * / 都有支持
期间有个extract 函数还是很有用的,我们先来看看几个例子:[code]
postgres=# select extract(epoch from '1970-01-01'::timestamp) ;
date_part
-----------
0
(1 row)
postgres=# select extract(epoch from '1970-01-01 00:00:01'::timestamp) ;
date_part
-----------
1
(1 row)
postgres=# select extract(epoch from now()-'2013-07-01'::timestamp) ;
date_part
----------------
1936767.072764
(1 row)
postgres=#
[/code]上面的例子是求出从1970-01-01 00:00:00 开始的秒数
extract 函数的功能是从时间中抽出相应的字段
他的使用格式:
EXTRACT(field FROM source)
其中field 包含以下几个值:
century: 世纪
postgres=# select extract(century from '2013-07-01'::date) ;
date_part
-----------
21
(1 row)
day : 一个月里的第几天[code]
postgres=# select extract(day from '2013-07-23'::date) ;
date_part
-----------
23
(1 row)
postgres=# select extract(day from '2013-07-23 09:15:23'::timestamp) ;
date_part
-----------
23
(1 row)
postgres=# select extract(day from '2013-07-23 09:15:23'::date) ;
date_part
-----------
23
(1 row)
postgres=# select extract(day from interval '40 days 3 hours' ) ;
date_part
-----------
40
(1 row)
[/code]decade : 10年期 ,第几个10年
postgres=# select extract(decade from '2013-07-23 09:15:23'::date) ;
date_part
-----------
201
(1 row)
dow 一周里的第几天 (sunday =0 saturday=6)
postgres=# select extract(dow from '2013-07-23 09:15:23'::date) ;
date_part
-----------
2
(1 row)
postgres=# select extract(dow from '2013-07-21 09:15:23'::date) ;
date_part
-----------
0
(1 row)
doy : 一年里的第几天(1-365/366)
postgres=# select extract(doy from '2013-07-21 09:15:23'::date) ;
date_part
-----------
202
(1 row)
hour: 一天里小时数(0-23)
postgres=# SELECT EXTRACT(HOUR FROM TIMESTAMP '2013-07-21 09:15:23');
date_part
-----------
9
(1 row)
postgres=# select extract(hour from '2013-07-21 09:15:23'::date) ;
date_part
-----------
0
(1 row)
注意这里,因为我们把'2013-07-21 09:15:23'::date) 转为date 类型是没有小时的,所以返回0 ,上面的timestamp 是有小时的,正确返回
isodow : ISO 标准一周的天数 sunday=7 monday=1
postgres=# select extract(isodow from '2013-07-21 09:15:23'::date) ;
date_part
-----------
7
(1 row)
postgres=# select extract(dow from '2013-07-21 09:15:23'::date) ;
date_part
-----------
0
(1 row)
postgres=# select extract(isodow from '2013-07-21 09:15:23'::timestamp) ;
date_part
-----------
7
(1 row)
isoyear: ISO 标准的年 : (
ISO标准的纪年是从周一开始,以一月4号之前的周一为新的纪年的开始,跟公元纪年有区别,所以一年的一月份的前几天,或者12月的后几天可能会跟公元纪年法有区别:
postgres=# select extract(isoyear from '2013-01-01'::date) ;
date_part
-----------
2013
(1 row)
postgres=# select extract(isoyear from '2012-12-31'::date) ;
date_part
-----------
2013
(1 row)
postgres=# select extract(isoyear from '2012-12-30'::date) ;
date_part
-----------
2012
(1 row)
postgres=# select extract(dow from '2012-12-31'::date) ;
date_part
-----------
1
(1 row)
microseconds: 微秒
用微秒标识的 秒 的部分,包括后面小数部分:
postgres=# select extract(microseconds from interval '3 days 5 mins 3.5 sec') ;
date_part
-----------
3500000
(1 row)
postgres=# select extract(microseconds from '2013-07-21 09:15:23'::timestamp)
postgres-# ;
date_part
-----------
23000000
(1 row)
millennium : 千禧年 ,千年纪年
目前是21世纪,第3个千禧年
postgres=# select extract(millennium from '2013-07-21 09:15:23'::timestamp) ;
date_part
-----------
3
(1 row)
minute: 分钟(0-59)
postgres=# select extract(minute from '2013-07-21 09:15:23'::timestamp) ;
date_part
-----------
15
(1 row)
month : 月份 对timestamp 类型 返回1-12, 对interval 类型返回0-11
postgres=# select extract(month from '2013-07-21 09:15:23'::timestamp) ;
date_part
-----------
7
(1 row)
postgres=# select extract(month from interval ' 7 months 5 days' ) ;
date_part
-----------
7
(1 row)
postgres=# select extract(month from interval ' 5 days' ) ;
date_part
-----------
0
(1 row)
postgres=# select extract(month from interval ' 12 months 5 days' ) ;
date_part
-----------
0
(1 row)
postgres=# select extract(month from interval ' 11 months 5 days' ) ;
date_part
-----------
11
(1 row)
quarter : 季度
postgres=# select extract(quarter from '2013-07-21 09:15:23'::timestamp) ;
date_part
-----------
3
(1 row)
postgres=# select extract(quarter from '2013-06-21 09:15:23'::timestamp) ;
date_part
-----------
2
(1 row)
second : 秒 (0-59)
postgres=# select extract(second from '2013-06-21 09:15:23'::timestamp) ;
date_part
-----------
23
(1 row)
week : 周记
postgres=# select extract(week from '2013-06-21 09:15:23'::timestamp) ;
date_part
-----------
25
(1 row)
year: 年纪
postgres=# select extract(year from '2013-06-21 09:15:23'::timestamp) ;
date_part
-----------
2013
(1 row)
详细解决方案
PG extract 函数示范
热度:243 发布时间:2016-05-05 08:15:15.0
相关解决方案
- PHP初记(2) - extract()方法
- 回到日期的某个域――extract()函数
- 无缘无故出现这个错误:JDBCConnectionException: could not extract ResultSet
- GoldenGate 如何实现 extract,replicat 进程开机启动
- PHP初记(2) - extract()方法
- PG extract 函数示范
- Unix / Linux: Extract Tar File with Absolute Path
- Could not extract response: no suitable HttpMessageConverter found for response type [class java.lan
- org.hibernate.exception.SQLGrammarException: could not extract ResultSet
- python pandas利用str.extract()方法处理标签
- Error: Failure while executing; `tar --extract --no-same-owner --file /Users/wangchuangyan/Library/C
- scrapy爬虫框架xpath.extract()[0]错误记录:IndexError: list index out of range
- 【论文阅读】Learning to Extract Attribute Value from Product via Question Answering: A Multi-task Approach
- could not extract ResultSet
- 解决fabric “Authentication failed: failed classifying identity: Unable to extract msp“问题
- 关于hibernate 实体没有主键调用save方法报错could not extract ResultSet的解决办法
- could not extract ResultSet; nested exception is org.hibernate.exception.GenericJDBCException: could
- spring hibernate jpa SQLGrammarException:could not extract ResultSet
- webpack学习笔记-5-extract-text-webpack-plugin
- Webpack 4: mini-css-extract-plugin + sass-loader + splitChunks sass打包未遂
- webpack学习7 问题解决 mini-css-extract-plugin的背景图的解决方法及css文件夹
- numpy选取满足特定条件的元素 numpy.extract
- jpa语句:nested exception is org.hibernate.exception.GenericJDBCException: could not extract ResultSet
- mini-css-extract-plugin在vue-cli4中的使用(抽取css)
- Failure [INSTALL_FAILED_NO_MATCHING_ABIS: Failed to extract native libraries, res=-113]
- 重构改善既有代码设计--重构手法01:Extract Method (提炼函数)