PG extract 函式示例
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)
期間有個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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- EXTRACT() 函式函式
- Pick和Extract的區別示例
- Oracle日期格式化以及extract函式的使用Oracle函式
- 關於在PostgreSQL中使用extract函式以及epochSQL函式
- MySql之json_extract函式處理json欄位MySqlJSON函式
- postgresql pg_xlog_location_diff 函式SQL函式
- java 回撥函式示例Java函式
- Python內建函式示例Python函式
- PG wal日誌LSN相關函式函式
- PostgreSQL 原始碼解讀(149)- PG Tools#1(pg_basebackup主函式)SQL原始碼函式
- Sanic response stream() 函式用法和示例函式
- Sanic response redirect() 函式用法和示例函式
- Sanic response raw() 函式用法和示例函式
- Sanic response file() 函式用法和示例函式
- Sanic response json() 函式用法和示例JSON函式
- Sanic response html() 函式用法和示例HTML函式
- Sanic response text() 函式用法和示例函式
- C語言庫函式及示例C語言函式
- Mysql中常用函式的使用示例MySql函式
- Go 官方包函式學習及示例Go函式
- CUDA函式的概念、種類和示例函式
- Python常見工廠函式用法示例Python函式
- Sanic response file_stream() 函式用法和示例函式
- Python | Python常用函式、方法示例總結(API)Python函式API
- PostgreSQL 原始碼解讀(150)- PG Tools#2(BaseBackup函式)SQL原始碼函式
- PG啟動流程StartupXlog函式回放日誌前處理函式
- makefile--變數與函式的綜合示例變數函式
- 數學建模例題2.20 enumerate()函式使用示例函式
- useRoute 函式的詳細介紹與使用示例函式
- Python3之遞迴函式簡單示例Python遞迴函式
- 遞迴示例-展開編號(Excel函式集團)遞迴Excel函式
- MKL稀疏矩陣運算示例及函式封裝矩陣函式封裝
- Swap函式的寫法及其常見錯誤示例函式
- 數學建模例題例 2.21 map()函式使用示例函式
- 數學建模例題例 2.22 filter()函式使用示例Filter函式
- 數學建模例題例 2.24 zip()函式使用示例函式
- REGEXP_EXTRACT
- 7.86 EXTRACT (XML)XML
- 7.85 EXTRACT (datetime)