Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy NULL values present in csv file to postgres

Tags:

I have csv file (y.csv) in the folowing format:

 's', '1999-10-10', '1999-12-12'  'b', '99-10-10 BC', '1-10-10 BC'  'c', 'NULL', 'NULL' 

I have a few NULL values (for date) in it which I have indicated through the string 'NULL'.

I am trying to copy the csv file into postgres. For doing so I have created a table:

create table r (id character varying(255), timebegin date, timeend date); 

Now I am trying to copy the above .csv file into postgres using the command

copy r from '/home/y.csv' delimiter ',' csv; ERROR:  invalid input syntax for type date: " 'NULL'" CONTEXT:  COPY r, line 1, column timebegin: " 'NULL'" 

On doing so I am getting an error with NULL. Can someone please help me figure out the error and correct it.

like image 495
Jannat Arora Avatar asked Sep 26 '13 17:09

Jannat Arora


People also ask

How do I pass a NULL value in a csv file?

Empty Strings and NULL Values In CSV files, a NULL value is typically represented by two successive delimiters (e.g. ,, ) to indicate that the field contains no data; however, you can use string values to denote NULL (e.g. null ) or any unique string.

How do I get NULL records in PostgreSQL?

Example - With INSERT StatementINSERT INTO contacts (first_name, last_name) SELECT first_name, last_name FROM employees WHERE employee_number IS NULL; This PostgreSQL IS NULL example will insert records into the contacts table where the employee_number contains a NULL value.

Does Postgres treat empty string as NULL?

PostgreSQL databases treat empty strings and NULL as different.


1 Answers

Have you tried it?

 copy r from '/home/y.csv' delimiter ',' csv WITH NULL AS 'null'; 
like image 132
sqlint Avatar answered Oct 21 '22 15:10

sqlint