postgres時間轉換函數


函數 返回類型 描述 例子
to_char(timestamp, text) text 把時間戳轉換成字串 to_char(current_timestamp, 'HH12:MI:SS')
to_char(interval, text) text 把時間間隔轉為字串 to_char(interval '15h 2m 12s', 'HH24:MI:SS')
to_char(int, text) text 把整數轉換成字串 to_char(125, '999')
to_char(double precision, text) text 把實數/雙精度數轉換成字串 to_char(125.8::real, '999D9')
to_char(numeric, text) text 把 numeric 轉換成字串 to_char(-125.8, '999D99S')
to_date(text, text) date 把字串轉換成日期 to_date('05 Dec 2000', 'DD Mon YYYY')
to_timestamp(text, text) timestamp with time zone 把字串轉換成時間戳 to_timestamp('05 Dec 2000', 'DD Mon YYYY')
to_timestamp(double precision) timestamp with time zone 把 UNIX 紀元轉換成時間戳 to_timestamp(200120400)
to_number(text, text) numeric 把字串轉換成 numeric to_number('12,454.8-', '99G999D9S')

postgresql 相關datetime:

1,date_trunc('month', now())

2,now()-interval '1 month'

 

 

1. decode 用 case when a=1 then b else c end
2. 最后一天  to_date(? +'1 mons'::interval,'yyyy-mm') -1
3. 第一天 to_date(?,'yyyy-mm') , date_trunc('month',?)
4.字段別名 加上 as
5.子查詢一定要用別名
6.取子樹 ,postgresql需人自己寫函數,或者用一些有結構特性字段如1.1,1.1.1,1.1.2  來代替

7.trunc(im.createdate) 可改為date_trunc('day',createdate)
  date_trunc 與oracle的trunc很像,還可以
  SELECT date_trunc('hour', TIMESTAMP '2001-02-16 20:38:40');
  Result: 2001-02-16 20:00:00+00

  SELECT date_trunc('year', TIMESTAMP '2001-02-16 20:38:40');
  Result: 2001-01-01 00:00:00+00
8.postgres-當日, 帶時分秒,now()  oracle- sysdate
9 postgres-當日,不帶時分秒current_date,oracle  to_char(sysdate,'YYYY-MM-DD)
10.nvl 全部替換成 coalesce  如 coalesce(im.invoiceamount,0)
11.小數據位數round,例保留一位小數 用select round(2.16,1)  Result: 2.2 會四舍五入
12 轉志數據類型用::數據類型,如to_number() 改用::numeric
13. 如果取子樹,不包含自身,使用函數時,第二個參數取2,如,getorgantree(?,2)
14.項目樹函數第一個參數是id,其他的如果地區編碼,稅務機關分別有參數為編碼的,如get..tree(code,level),參數為id的,如get..treebyid(id,level)
15.修改表時,表名不能帶別名,如update taxpayer_cognizance_invoice tc   這里taxpayer_cognizance_invoice 不能帶別名tc


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM