S Leon
S Leon

Reputation: 331

PostgreSQL : converting existing double precision column to integer data type

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

Answers (1)

user330315
user330315

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

Related Questions