HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlMinor

How to change the datatype of a column from integer to money?

Submitted by: @import:stackexchange-dba··
0
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.

ALTER TABLE products 
    ALTER COLUMN price TYPE money


and:

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.

ALTER TABLE products ALTER COLUMN price TYPE money using price::text::money


Non-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::money

Context

StackExchange Database Administrators Q#6205, answer score: 9

Revisions (0)

No revisions yet.