If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > PostgreSQL > Copy "" as null to timestamp and integer

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-12-11, 00:19
sushmita_mishra sushmita_mishra is offline
Registered User
 
Join Date: Apr 2011
Posts: 10
Copy "" as null to timestamp and integer

Hi All,

I have a CSV file where the null values are present as "". I am trying to copy those contents into my postgres table.

My Query is:
COPY table1 FROM 'data.csv' with delimiter ',' csv header null as '';

Error :
ERROR: invalid input syntax for type timestamp: ""
CONTEXT: COPY table1, line 2, column login_dt: ""

where login_dt is a column with type timestamp without time zone.

Guide me how can I load the data properly.

Thanks in advance
Reply With Quote
  #2 (permalink)  
Old 12-22-11, 16:51
loquin loquin is offline
Super Moderator
 
Join Date: Jun 2004
Location: Arizona, USA
Posts: 1,797
One approach is to first import the data into a staging table.

Then, run a query that copies data from the staging table into the final table, using the case construct or the NULLIF function in the select subquery, to insert a null in the target field when the source field is an empty string. (or, more accurately, when the staging table source field contains just the two quotation characters "")
Code:
Select field1, NULLIF(field2, '""') AS field2, NULLIF(field3, ''"") as field3 FROM staging_table
__________________
Lou
使大吃一惊
"Lisa, in this house, we obey the laws of thermodynamics!" - Homer Simpson
"I have my standards. They may be low, but I have them!" - Bette Middler
"It's a book about a Spanish guy named Manual. You should read it." - Dilbert

Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On