postgreSQL除法保留小數


--1 例子
postgres=# select 1/4;
 ?column? 
----------
        0
(1 row)

        在PG里如果想做除法並想保留小數,用上面的方法卻行不通,因為"/" 運算結果為取整,並且會截掉小數部分。
 

--2 類型轉換
postgres=# select round(1::numeric/4::numeric,2);
 round 
-------
  0.25
(1 row)

  備注:類型轉換后,就能保留小數部分了。


--3 也可以通過 cast 函數進行轉換
postgres=# select round( cast ( 1 as numeric )/ cast( 4 as numeric),2);
 round 
-------
  0.25
(1 row)


--4 關於 cast 函數的用法
postgres=# SELECT substr(CAST (1234 AS text), 3,1);
 substr 
--------
 3
(1 row)


免責聲明!

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



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