Importing CSV into postgres blank columns contain quotes

Questions and answers regarding general SQL and backend databases

Importing CSV into postgres blank columns contain quotes

Postby datacraftsteve » Tue Apr 15, 2014 8:49 pm

Is there a way to import a csv file into a postgres 9.1 table in a way that columns
in the csv file that have no data will be in the postgres table as blanks instead of single quotes?

Here are the details..

my csv file looks like this:
firstname,middlename,lastname,jr
"John","M","Smith","III"
"Mary","","Smith",""

My table columns look like this:
firstname character varying (25),
middlename character varying (15),
lastname character varying (25),
jr character varying (4)

My copy statement looks like this:
COPY akamaitest FROM 'C:\Program Files (x86)\PostgreSQL\9.1\data\akamaitest.CSV' WITH (FORMAT 'csv', HEADER, NULL '');

The import works perfectly except that the columns for Mary that have no data (middlename and jr) contain two single quotes: ''
I'm trying in vain to modify my copy statement and my table schema so the columns that have no data show up in the table as blanks not quotes.

I've also tried altering the table columns for middlename and jr by adding the NULL keyword like this:
middlename character varying (15) NULL,

But that made no difference, the two single quotes still are placed into the columns with no data.

Any suggestions would be really appreciated.
datacraftsteve
 
Posts: 5
Joined: Thu Apr 10, 2014 10:13 am

Re: Importing CSV into postgres blank columns contain quotes

Postby datacraftsteve » Wed Apr 16, 2014 12:03 am

Okay, I think this is solved. Or more accurately, there was no problem to begin with.

It occurred to me that perhaps pgadmin III displays the two single quotes in the fields where there is no data as just the way it does it. So, I created a simple Servoy form with just firstname, middlename, lastname and jr. And what do you know, the single quotes are not displayed in the middlename and jr in the form for the same records where it is shown in pgadmin III.

So, I guess I don't have an issue after all, except for maybe being just way too inexperienced.

Thanks and my apologies to anyone who spent time trying to figure this out. But, at least it's here in the forum for the next idiot like me who comes along.

Steve
datacraftsteve
 
Posts: 5
Joined: Thu Apr 10, 2014 10:13 am


Return to SQL Databases

Who is online

Users browsing this forum: No registered users and 2 guests