0

I have a CSV with 3 columns ; text, integer, text. When i import these all the data is enclosed in double brackets. I'm not sure why. Is there away to remove these on import using COPY? Or should i import as text and then remove them using select replace ?

Edit; Code for copy

copy tavle from 'file.csv' with delimiter ',' CSV encoding 'windows-1251'

Sample of raw csv data;

"TY373543","4510069420","A"
"ND4368","4510093370","A"
"TY152401","4510093370","A"
"TY384054","4510069421","A"
mapping dom
  • 1,737
  • 4
  • 27
  • 50

1 Answers1

3

If your target table and csv file have the same structure, I fail to see how it is possible. Follow this example:

Considering the following CSV file ...

x, y, z
"TY373543","4510069420","A"
"ND4368","4510093370","A"
"TY152401","4510093370","A"
"TY384054","4510069421","A"

and the following table structure ...

CREATE TABLE t (a text, b bigint, c text);

Just execute this command:

COPY t FROM '/tmp/file.csv' CSV HEADER;

And here is your data:

   SELECT * FROM t;
    a     |     b      | c 
----------+------------+---
 TY373543 | 4510069420 | A
 ND4368   | 4510093370 | A
 TY152401 | 4510093370 | A
 TY384054 | 4510069421 | A
(4 Zeilen)
Jim Jones
  • 18,404
  • 3
  • 35
  • 44
  • Thank you, removing the delimiter and optional parameters makes it work. Why is that? – mapping dom Apr 30 '18 at 12:33
  • @mappingdom I tried several different copy commands and none of them reproduced the behavior you mentioned, even with the delimiter option `COPY t FROM '/tmp/file.csv' WITH DELIMITER ',' CSV HEADER;` Really puzzling ... – Jim Jones Apr 30 '18 at 12:51