はじめに
Postgresのdouble presision型のデータに対し、round関数で四捨五入するとうまくいかないため、対応した時のメモ
環境
- Postgres 11
そのままやると
select round(col1,5) from test where id=1
するとこんなエラーが。
ERROR: function round(double precision, integer) does not exist
LINE 7: select round(col1,5) from test where ...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
SQL state: 42883
Character: 290
原因
どうやら、Postgresにはdouble precisionを四捨五入する関数がないらしい。理由は参考URLに書いてある模様。
対応
無理やりnumericに変換してround関数に渡す♪
select round(CAST(col1 as numeric),5) from test where id=1