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

原文地址:https://www.cnblogs.com/chjbbs/p/3824689.html