[英]Postgres interval in total months
我有一個帶有間隔數據的Postgres表,例如:
我想創建一個顯示值的視圖,但以總月份為單位(因此,我的正確值是:0、1、1.5、9、12)。 我試過使用:
EXTRACT(MONTHS FROM "MyTable"."Interval"::INTERVAL)
然而,這產生了不正確的結果(0,1,1,9,0)。
是否還有另一個函數可以顯示期望值?
好吧, extract
函數從日期/時間值中檢索子字段,例如年或小時,而不是所需的總時間,但是我們可以自己構造結果,如下所示:
postgres=# select * from month_table ;
month
------------
0 month
1 month
1.5 months
9 months
1 year
2 years
1.5 years
(7 rows)
postgres=# select
case when month ~ 'month' then regexp_replace(month,' month.*','')::numeric
when month ~ 'year' then regexp_replace(month,' year.*','')::numeric * 12 end as months
from
month_table;
months
--------
0
1
1.5
9
12
24
18.0
(7 rows)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.