Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL : converting existing double precision column to integer data type

Tags:

postgresql

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 ?

like image 542
S Leon Avatar asked Dec 25 '22 01:12

S Leon


1 Answers

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

like image 183
a_horse_with_no_name Avatar answered Jan 14 '23 02:01

a_horse_with_no_name