pg 对时间的处理还是很灵活的, + - * / 都有支持期间有个extract 函数还是很有用的,我们先来看看几个例子:[code]postgres=# select extract(epoch from 1970-01-01::timestamp) ; ...
pg 对时间的处理还是很灵活的, + - * / 都有支持期间有个extract 函数还是很有用的,我们先来看看几个例子:[code]postgres=# select extract(epoch from '1970-01-01'::timestamp) ; postgres=# select extract(epoch from '1970-01-01 00:00:01'::timestamp) ; postgres=# select extract(epoch from now()-'2013-07-01'::timestamp) ; postgres=# extract 函数的功能是从时间中抽出相应的字段他的使用格式:其中field 包含以下几个值: century: 世纪 postgres=# select extract(century from '2013-07-01'::date) ; day : 一个月里的第几天[code]postgres=# select extract(day from '2013-07-23'::date) ; postgres=# select extract(day from '2013-07-23 09:15:23'::timestamp) ; postgres=# select extract(day from '2013-07-23 09:15:23'::date) ; postgres=# select extract(day from interval '40 days 3 hours' ) ;postgres=# select extract(decade from '2013-07-23 09:15:23'::date) ; postgres=# select extract(dow from '2013-07-21 09:15:23'::date) ; postgres=# select extract(doy from '2013-07-21 09:15:23'::date) ; hour: 一天里小时数(0-23) postgres=# SELECT EXTRACT(HOUR FROM TIMESTAMP '2013-07-21 09:15:23'); postgres=# select extract(hour from '2013-07-21 09:15:23'::date) ; 注意这里,因为我们把'2013-07-21 09:15:23'::date) 转为date 类型是没有小时的,所以返回0 ,上面的timestamp 是有小时的,正确返回postgres=# select extract(isodow from '2013-07-21 09:15:23'::date) ; postgres=# select extract(dow from '2013-07-21 09:15:23'::date) ; postgres=# select extract(isodow from '2013-07-21 09:15:23'::timestamp) ; isoyear: ISO 标准的年 : (ISO标准的纪年是从周一开始,以一月4号之前的周一为新的纪年的开始,跟公元纪年有区别,所以一年的一月份的前几天,或者12月的后几天可能会跟公元纪年法有区别: postgres=# select extract(isoyear from '2012-12-31'::date) ; postgres=# select extract(isoyear from '2012-12-30'::date) ; postgres=# select extract(dow from '2012-12-31'::date) ; 用微秒标识的 秒 的部分,包括后面小数部分: postgres=# select extract(microseconds from interval '3 days 5 mins 3.5 sec') ;postgres=# select extract(microseconds from '2013-07-21 09:15:23'::timestamp) millennium : 千禧年 ,千年纪年目前是21世纪,第3个千禧年postgres=# select extract(millennium from '2013-07-21 09:15:23'::timestamp) ; postgres=# select extract(minute from '2013-07-21 09:15:23'::timestamp) ; month : 月份 对timestamp 类型 返回1-12, 对interval 类型返回0-11postgres=# select extract(month from '2013-07-21 09:15:23'::timestamp) ; postgres=# select extract(month from interval ' 7 months 5 days' ) ; postgres=# select extract(month from interval ' 12 months 5 days' ) ; postgres=# select extract(month from interval ' 11 months 5 days' ) ; postgres=# select extract(quarter from '2013-07-21 09:15:23'::timestamp) ; postgres=# select extract(quarter from '2013-06-21 09:15:23'::timestamp) ; postgres=# select extract(second from '2013-06-21 09:15:23'::timestamp) ; postgres=# select extract(year from '2013-06-21 09:15:23'::timestamp) ;
海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com
原标题:PG extract 函数示例
关键词:函数
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。