轉自: http://blog.csdn.net/yufenghyc/article/details/45869509函數
--1 例子
postgres=# select 1/4;
?column?
----------
0
(1 row)post
在PG裏若是想作除法並想保留小數,用上面的方法卻行不通,由於"/" 運算結果爲取整,而且會截掉小數部分。
.net
--2 類型轉換
postgres=# select round(1::numeric/4::numeric,2);
round
-------
0.25
(1 row)blog
備註:類型轉換後,就能保留小數部分了。ast
--3 也能夠經過 cast 函數進行轉換
postgres=# select round( cast ( 1 as numeric )/ cast( 4 as numeric),2);
round
-------
0.25
(1 row)select
--4 關於 cast 函數的用法
postgres=# SELECT substr(CAST (1234 AS text), 3,1);
substr
--------
3
(1 row)方法