PG extract 函式示例

babyyellow發表於2013-07-23
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)



來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/133735/viewspace-766907/,如需轉載,請註明出處,否則將追究法律責任。

相關文章