在 PostgreSQL 查询中将金钱转换为双倍

Convert Money to Double in PostgreSQL Query

我的 PostgreSQL 数据库中有一个 table 类型为 money 的列。我想将数据连接到 Google Data Studio,但它不支持 money 类型的数据。

有没有办法在这样的查询或任何其他等效查询中将 money 类型转换为 doublebigint

SELECT todouble(maintenance.cost) FROM maintenance

谢谢!

最自然的选择是numeric:

select 123.45::money::numeric;

 numeric
---------
  123.45
(1 row)

您也可以使用 integerbigint,但您必须注意处理小数部分。根据 the documentation::

,不要尝试 realdouble precision

Floating point numbers should not be used to handle money due to the potential for rounding errors.

你可以使用::numeric::float8这个来转换它。所以变成了

SELECT maintenance.cost::numeric::float8 FROM maintenance

您可以在下面的文档中看到它: https://www.postgresql.org/docs/current/datatype-money.html