From: | Steve Crawford <scrawford(at)pinpointresearch(dot)com> |
---|---|
To: | Patrick Schneider <patrick(dot)schneider(at)debeka(dot)de> |
Cc: | pgsql-general(at)postgresql(dot)org |
Subject: | Re: Conversion of columns during CSV Import |
Date: | 2012-06-29 17:27:45 |
Message-ID: | 4FEDE591.7000503@pinpointresearch.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
On 06/29/2012 12:54 AM, Patrick Schneider wrote:
> Hello,
>
> is there any possibility to convert special columns during an CSV
> import via COPY?
> For example:
>
> HELLO;WORLD;9999;011001
>
> 9999 should be converted to a character field
> 011001 to the date 10th January 2001
>
> For the moment the only idea we have is, to import the CSV into a TEMP
> table and
> perform the conversion by a select from the temp to the target table.
Technically everything coming in from the CSV is text and it will be
converted to the data-type in your PostgreSQL table during the import
unless such conversion is not possible in which case it will throw an
error. 9999 will import as text to a text/character field. You could
also import it to an int or numeric provided all data in that column
will convert to the specified type.
Similarly, "011001" will be interpreted per ISO 8601 as 10 January 2001.
But your sample data is ambiguous (month 01 and year 01). If the data
ordering is YMD you are fine. MDY will be problematic and may require a
preprocessing step either inside or outside PostgreSQL.
Given the above data:
create table foo (a text, b text, c text, d date);
An input table (with some data to show where the month and the year
really are:
HELLO;WORLD;9999;011001
Goodbye;World;0000;120629
Copy the data:
\copy foo from foo.txt csv delimiter ';'
Profit:select * from foo;
a | b | c | d
---------+-------+------+------------
HELLO | WORLD | 9999 | 2001-10-01
Goodbye | World | 0000 | 2012-06-29
Cheers,
Steve
From | Date | Subject | |
---|---|---|---|
Next Message | Alban Hertroys | 2012-06-29 17:47:21 | Re: Conversion of columns during CSV Import |
Previous Message | Raymond O'Donnell | 2012-06-29 17:10:26 | Re: Conversion of columns during CSV Import |