Reputation: 331
I have a column in PostgreSQL that contains numbers. The column's data type is double precision, and the numbers have a lot of decimal digits. I don't need that level of accuracy. Integers would be enough.
How do I redefine the column as having an integer datatype, while rounding the current existing data to obtain integers ?
Upvotes: 5
Views: 13501
Reputation:
Just alter the table, Postgres will automatically cast the double values to integer by rounding the appropriately:
ALTER TABLE foo ALTER COLUMN some_column TYPE integer;
This will essentially do the same as:
ALTER TABLE foo ALTER COLUMN some_data TYPE integer
USING round(some_data)::integer;
SQLFiddle: http://sqlfiddle.com/#!15/4ea3c/1
Upvotes: 12