Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cast double precision as currency in postgresql

Tags:

postgresql

I am having trouble casting a double precision value to money.

SELECT scrap_cost.year AS year
, scrap_cost.weekly AS week
, scrap_cost.scrap_cost::MONEY
, labor_cost.labor_cost::MONEY
FROM ...

returns

ERROR:  cannot cast type double precision to money
LINE 4: ..., scrap_cost.scrap_cost::MONEY, labor_cost.labor_cost::money
                                                            ^
********** Error **********

ERROR: cannot cast type double precision to money
SQL state: 42846
Character: 117
like image 361
Daniel L. VanDenBosch Avatar asked Dec 18 '22 04:12

Daniel L. VanDenBosch


1 Answers

What I did to solve this was CAST it as a numeric first

SELECT 
    scrap_cost.year AS year
    , scrap_cost.weekly AS week
    , scrap_cost.scrap_cost::MONEY 
    , labor_cost.labor_cost::NUMERIC::MONEY
FROM ...

I found the solution in the postgres documentation

like image 193
Daniel L. VanDenBosch Avatar answered Dec 29 '22 17:12

Daniel L. VanDenBosch