dbTalk Databases Forums  

[ADMIN] Trouble w/ COPY command

mailing.database.pgsql-admin mailing.database.pgsql-admin


Discuss [ADMIN] Trouble w/ COPY command in the mailing.database.pgsql-admin forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Wells Oliver
 
Posts: n/a

Default [ADMIN] Trouble w/ COPY command - 04-14-2012 , 05:45 PM






I have CSV files blowing up because they have double quotes around numeric
values: " 1.34" yields ERROR: invalid input syntax for type numeric: "
1.34"

And occasionally these are empty, and you see invalid syntax for type
numeric: " "

Are there flags I can pass to COPY to avoid this? There are properly quoted
string values elsewhere in the final, so I'm reticent to strip out double
quote characters or anything.

Thank you.

--
Wells Oliver
wellsoliver (AT) gmail (DOT) com

Reply With Quote
  #2  
Old   
Greg Williamson
 
Posts: n/a

Default Re: [ADMIN] Trouble w/ COPY command - 04-14-2012 , 06:17 PM






Wells Oliver asked:


Quote:
I have CSV files blowing up because they have double quotes around numericvalues: " 1.34" yields*ERROR: *invalid input syntax for type numeric: " 1.34"

And occasionally these are empty, and you see invalid syntax for type numeric: " "

Are there flags I can pass to COPY to avoid this? There are properly quoted string values elsewhere in the final, so I'm reticent to strip out doublequote characters or anything.


There's no easy command line method that I know of. Perhaps if you can detect patterns some sed / awk might help.

I'd probably load the CSV file into a temporary work table with the columnsthat are giving you issues defined as type "text." Then use SQL to remove the offending quotation marks from the afflicted columns. Create new columns of type numeric in the table, and copy the values from the text variant to the numeric ones with the proper cast one at a time; if it blows up you go back to resolving issues. Then copy the table using the numeric columns to the desired destination.

Hopefully others might have better ideas ... best o' luck!


Greg Williamson


--
Sent via pgsql-admin mailing list (pgsql-admin (AT) postgresql (DOT) org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-admin

Reply With Quote
  #3  
Old   
Tom Lane
 
Posts: n/a

Default Re: [ADMIN] Trouble w/ COPY command - 04-14-2012 , 07:01 PM



Wells Oliver <wellsoliver (AT) gmail (DOT) com> writes:
Quote:
I have CSV files blowing up because they have double quotes around numeric
values: " 1.34" yields ERROR: invalid input syntax for type numeric: "
1.34"
Is that an exact copy of the error message? Because there are double
quotes in that error message's text; what you are showing does not
suggest that they are part of the data value. I'm wondering about
non-visible characters myself.

Quote:
And occasionally these are empty, and you see invalid syntax for type
numeric: " "
Well, that you're going to have to do something else about. Possibly
you could treat the empty string as null? (See the 'null string'
option to COPY.)

regards, tom lane

--
Sent via pgsql-admin mailing list (pgsql-admin (AT) postgresql (DOT) org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-admin

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.