snippetsqlMinor
How to change the datatype of a column from integer to money?
Viewed 0 times
moneythedatatypecolumnhowfromchangeinteger
Problem
I am attempting to convert a PostgreSQL table column from integer to money, but I am receiving the error:
cannot cast type MyColumn to money
I have tried these two statements, but just haven't found any real example on how to really do it.
and:
How can you change the datatype of a PostgreSQL column from integer to money?
cannot cast type MyColumn to money
I have tried these two statements, but just haven't found any real example on how to really do it.
ALTER TABLE products
ALTER COLUMN price TYPE moneyand:
ALTER TABLE products
ALTER COLUMN price TYPE money USING to_char(price, '999.99')How can you change the datatype of a PostgreSQL column from integer to money?
Solution
Just try the following command.
Non-quoted numeric values can be converted to money by casting the numeric value to text and then money
ALTER TABLE products ALTER COLUMN price TYPE money using price::text::moneyNon-quoted numeric values can be converted to money by casting the numeric value to text and then money
Code Snippets
ALTER TABLE products ALTER COLUMN price TYPE money using price::text::moneyContext
StackExchange Database Administrators Q#6205, answer score: 9
Revisions (0)
No revisions yet.