0

I am trying to import data into PostgreSQL with NULL values in Windows 7.

"Code1.2";"08";"ALCALA";"ACTIVO SERVICIO";"EN SERVICIO";"";"2017/04/08";1 
"Code1.3";"46";"BARCELONA";"ACTIVO SERVICIO";"EN SERVICIO";"";"2009/07/01";0

I don't want to import quotes "data" neither "" values.

CREATE TABLE IF NOT EXISTS public.TABLA1 ( 
    CODIGO text, 
    COD text, 
    MUNICIPIO text, 
    ESTADO text, 
    SITUACION text, 
    FECHA_INICIO date, 
    FECHA_FIN date, 
    CONTADOR numeric
);

If I import data with the following I can handle NULL values, but I have the "" in the data:

COPY public.TABLA1 (
    CODIGO, 
    COD, 
    MUNICIPIO, 
    ESTADO, 
    SITUACION, 
    FECHA_INICIO, 
    FECHA_FIN, 
    CONTADOR
) 
FROM 'C:\DATA\ORIGEN\fichero_prueba.csv' 
WITH (DELIMITER ';', NULL '""');

| CODIGO    | COD  | MUNICIPIO | ESTADO          |SITUACION    |FECHA_INICIO|...
| --------- | ---- | --------- | ----------------|-------------|------------|...
| "Code1.2" | "08" |"ALCALA"   |"ACTIVO SERVICIO"|"EN SERVICIO"|[null]      |...
| "Code1.3" | "46" |"BARCELONA"|"ACTIVO SERVICIO"|"EN SERVICIO"|[null]      |...
 ...

On the other hand If I import the data with the following, trying to leave the quotation marks out:

COPY public.TABLA1 ( 
    CODIGO,
    COD,
    MUNICIPIO,
    ESTADO,
    SITUACION,
    FECHA_INICIO,
    FECHA_FIN,
    CONTADOR
)
FROM 'C:\DATA\ORIGEN\fichero_prueba.csv'
WITH DELIMITER ';' CSV QUOTE '"' NULL '""';

ERROR: CSV quote character must not appear in the NULL specification

And not using NULL:

COPY public.TABLA1 (
    CODIGO,
    COD,
    MUNICIPIO,
    ESTADO,
    SITUACION,
    FECHA_INICIO,
    FECHA_FIN,
    CONTADOR
)
FROM 'C:\DATA\ORIGEN\fichero_prueba.csv'
WITH DELIMITER ';' CSV QUOTE '"';

ERROR: invalid input syntax for type date: «»

How can I import the data without the "" and handle NULL values in date types?

10
  • Por favor, habla en engles si posible. Este website esta usando engles y no español. Oct 1, 2017 at 15:40
  • @TimBiegeleisen I translated it
    – alisson
    Oct 1, 2017 at 16:40
  • I'm pretty sure the first is what you want, you're converting empty strings to NULL and the quotation marks are showing because those values are, in fact, strings. If that's not what you want then try to thoroughly explain the expected behavior.
    – oxfist
    Oct 1, 2017 at 18:11
  • @Oxfist I don't think so, because I had changed data manually (to make a test) and use it without the initial quotes and in the database I can see my text columns without quotes.
    – alisson
    Oct 1, 2017 at 18:19
  • @alisson I noticed that in the first one you didn't add QUOTE '"' to the COPY statement, maybe try that.
    – oxfist
    Oct 1, 2017 at 18:28

1 Answer 1

1

From one of your answers in the comments I assume you have access to the file (since you can change the extension from .dat to .csv) so I'd recommend opening the file in Excel (or any equivalent) and removing the quotes from the FECHA_INICIO column, so that instead of:

"Code1.2";"08";"ALCALA";"ACTIVO SERVICIO";"EN SERVICIO";"";"2017/04/08";1 
"Code1.3";"46";"BARCELONA";"ACTIVO SERVICIO";"EN SERVICIO";"";"2009/07/01";0

You'd have:

"Code1.2";"08";"ALCALA";"ACTIVO SERVICIO";"EN SERVICIO";;"2017/04/08";1 
"Code1.3";"46";"BARCELONA";"ACTIVO SERVICIO";"EN SERVICIO";;"2009/07/01";0

If you can do this then the following will insert the data to the table without the double quotes and with NULL in the FECHA_INICIO column:

COPY public.TABLA1 (
    CODIGO, 
    COD, 
    MUNICIPIO, 
    ESTADO, 
    SITUACION, 
    FECHA_INICIO, 
    FECHA_FIN, 
    CONTADOR
) 
FROM 'C:\DATA\ORIGEN\fichero_prueba.csv' 
WITH CSV DELIMITER ';' QUOTE '"';
3
  • I cannot open it in excel because it has more than 4.000.000 rows and the limit in excel is 1.000.000. I change the extension using powershell.
    – alisson
    Oct 1, 2017 at 19:51
  • 1
    @alisson from what I gathered reading the docs I think your solution is to modify the original file, e.g. you could use Vim or any text editor to replace ""; for ;, because QUOTE '"' and NULL '""' will definitely not work together in CSV mode, and to use QUOTE you have to be in CSV mode.
    – oxfist
    Oct 1, 2017 at 20:07
  • Furthermore, if you're confident that you won't find double quotes that need to be escaped, you could just use a text editor to remove every single double quote and I'm sure you'll get the result you want.
    – oxfist
    Oct 1, 2017 at 20:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.