Re: CSV files & empty strings

Lists: pgsql-general
From: Nate Randall <nate1604(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: CSV files & empty strings
Date: 2009-10-20 04:55:45
Message-ID: a4c6b1fe0910192155r1a2a4fa8w4e179c360073e2be@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

I am new to Postgre, having formerly used Filemaker Pro. I need to import
CSV files into Postgre which contain some empty string "" values in several
date-type fields. I am able to import the CSV files into Postgre with the
COPY FROM command if the datefields are converted to text fields in
Postgre. However, I need some method of "converting" the empty string ""
values into NULL values after import, so that I can change the date fields
back to date-type. Any suggestions on how this could be accomplished?

Thanks,

-Nate


From: Raymond O'Donnell <rod(at)iol(dot)ie>
To: Nate Randall <nate1604(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: CSV files & empty strings
Date: 2009-10-20 14:15:29
Message-ID: 4ADDC601.8070200@iol.ie
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On 20/10/2009 05:55, Nate Randall wrote:
> I am new to Postgre, having formerly used Filemaker Pro. I need to
> import CSV files into Postgre which contain some empty string "" values
> in several date-type fields. I am able to import the CSV files into
> Postgre with the COPY FROM command if the datefields are converted to
> text fields in Postgre. However, I need some method of "converting" the
> empty string "" values into NULL values after import, so that I can
> change the date fields back to date-type. Any suggestions on how this
> could be accomplished?

How about:

update <table> set <column> = null where <column> = '';

?

Ray.


From: Niklas Johansson <pgmailings(at)codecraft(dot)se>
To: Nate Randall <nate1604(at)gmail(dot)com>
Cc: pgsql <pgsql-general(at)postgresql(dot)org>
Subject: Re: CSV files & empty strings
Date: 2009-10-26 22:36:47
Message-ID: 25C480FD-49F1-4BEF-BC5E-53764F03C327@codecraft.se
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general


On 20 okt 2009, at 16.15, Raymond O'Donnell wrote:

> On 20/10/2009 05:55, Nate Randall wrote:
>> However, I need some method of "converting" the
>> empty string "" values into NULL values after import, so that I can
>> change the date fields back to date-type. Any suggestions on how
>> this
>> could be accomplished?
>
> How about:
>
> update <table> set <column> = null where <column> = '';

You can also do the update and the column data type change in one
fell swoop like so:

ALTER TABLE table_name ALTER COLUMN column_name
SET DATA TYPE DATE USING NULLIF(column_name, '')::DATE;

Sincerely,

Niklas Johansson