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 ?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With