8

I am importing 50 CSV data files into postgres. I have an integer field where sometimes the value is a regular number (comma-delimited) and sometimes it is in quotations and uses a comma for the thousands.

For instance, I need to import both 4 and "4,000".

I'm trying:

COPY race_blocks FROM '/census/race-data/al.csv' DELIMITER ',' CSV HEADER;

And get the error:

ERROR: invalid input syntax for integer: "1,133"

How can I do this?

2
  • 4
    You can import to a temp table such numbers as strings and then copy to a real table with apropriate conversion. Commented Sep 9, 2013 at 21:46
  • @IgorRomanchenko This should be an answer, it's exactly what I was going to suggest. Only say text rather than "strings", since this is PostgreSQL :) Commented Sep 9, 2013 at 22:15

1 Answer 1

5

Let's assume you have only one column in your data. First create temporary table with varchar column:

CREATE TEMP TABLE race_blocks_temp (your_integer_field VARCHAR);

Copy your data from file

COPY race_blocks_tmp FROM '/census/race-data/al.csv' DELIMITER ',' CSV HEADER;

Remove ',' from varchar field, convert data to numeric and insert into your table.

INSERT INTO race_blocks regexp_replace(your_integer_field, ',', '') :: numeric AS some_colun FROM race_blocks_tmp;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! My table had multiple columns, but I was still able to use regex_replace. This is what the final step looked like (after renaming my table as a temp table and creating a new table with sales_total in real format: insert into sales(id, product_id, manufacturer, category, product, customer_id, sale_date, sale_time, quantity, price, sale_total) select id, product_id, manufacturer, category, product, customer_id, sale_date, sale_time, quantity, price, regexp_replace(sale_total, ',', '') :: numeric from sales_temp; Also, storing sales_total as text (not VARCHAR) also worked.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.