Re: date problems

From: Michael Glaesemann <grzm(at)seespotcode(dot)net>
To: marchesini(at)unipg(dot)it
Cc: postgresql sql list <pgsql-sql(at)postgresql(dot)org>
Subject: Re: date problems
Date: 2007-08-30 14:39:47
Message-ID: 39419CF6-0708-46BE-9C90-2CDC950313F4@seespotcode.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql


On Aug 30, 2007, at 8:01 , ivan marchesini wrote:

> a lot of table have some timestamp fields containing data as DD/MM/
> YYYY,
> and I'm no able to copy this table into postgres... because it needs
> YYYY/MM/DD...
> I used \copy...

The input and output formats of dates is controlled by the datestyle
setting. You can easily change this to allow COPY (and I assume
\copy) to load the dates in their current format.

test=# create table dates (a_date date primary key);
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index
"dates_pkey" for table "dates"
CREATE TABLE
test=# copy dates (a_date) from stdin;
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself.
>> 2007/08/30
>> 2007/08/29
>> \.

No problem loading dates in YMD.

test=# copy dates (a_date) from stdin;
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself.
>> 08/28/2007
>> \.

No problem with MDY.

test=# copy dates (a_date) from stdin;
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself.
>> 27/08/2007
>> \.
ERROR: date/time field value out of range: "27/08/2007"
HINT: Perhaps you need a different "datestyle" setting.
CONTEXT: COPY dates, line 1, column a_date: "27/08/2007"

DMY fails.

test=# show datestyle;
DateStyle
-----------
ISO, MDY
(1 row)

The current datestyle is ISO for output and MDY for input. This
explains why '27/08/2007' failed.

test=# set datestyle to 'iso, dmy'; -- output still iso, input day-
month-year
SET
test=# show datestyle;
DateStyle
-----------
ISO, DMY
(1 row)

test=# copy dates (a_date) from stdin;
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself.
>> 27/08/2007
>> \.

Since the datestyle was changed, we can now input '27/08/2007'.

test=# select * from dates;
a_date
------------
2007-08-30
2007-08-29
2007-08-28
2007-08-27
(4 rows)

And there they are: all output in ISO format.

Hope this helps.

Michael Glaesemann
grzm seespotcode net

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Roberto Spier 2007-08-30 18:31:52 Re: date problems
Previous Message A. Kretschmer 2007-08-30 13:29:47 Re: date problems