ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

PG extract 函数示例

2020-05-14 14:55:04  阅读:345  来源: 互联网

标签:postgres 示例 part PG date extract select row


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)

 

转载于:

http://blog.itpub.net/133735/viewspace-766907/

标签:postgres,示例,part,PG,date,extract,select,row
来源: https://www.cnblogs.com/yusuf/p/12888583.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有