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
c++trunc函数5.⼦查询⼀定要⽤别名
6.取⼦树 ,postgresql需⼈⾃⼰写函数,或者⽤⼀些有结构特性字段如1.1,1.1.1,1.1.2  来代替
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,其他的如果地区编码,税务机关分别有参数为编码的,如(code,level),参数为id的,如byid(id,level)
15.修改表时,表名不能带别名,如update taxpayer_cognizance_invoice tc  这⾥taxpayer_cognizance_invoice 不能带别名tc

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。