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)




What Doesn't Kill Me Makes Me Stronger