PostgreSQL: convert existing double precision column to integer data type

I have a column in PostgreSQL that contains numbers. The column data type is double precision and numbers have many decimal digits. I don't need that level of accuracy. Wholes would be enough.

How can I redefine a column as an integer data type, rounding off the current existing data to get integers?

+3


source to share


1 answer


Just change the table, Postgres will automatically convert double values ​​to integer, rounding accordingly:

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

+7


source







All Articles