Re: [SQL] Stupid question about default time value - Mailing list pgsql-sql

From Tom Lane
Subject Re: [SQL] Stupid question about default time value
Date
Msg-id 20254.931791968@sss.pgh.pa.us
Whole thread Raw
In response to Stupid question about default time value  ("Nikolay Mijaylov" <nmmm@nsi.bg>)
List pgsql-sql
"Nikolay Mijaylov" <nmmm@nsi.bg> writes:
> I made a table such as:
> create teble x(
> i int2,
> d date default 'today',
> t time default 'now',
> dt datetime default 'now'
> );

> So these default values are not working as i think... they make the default
> values to be equal to time when table was created....

Yes, because the default values will normally be converted to the column
data type at the time the CREATE TABLE is done.  What you want is to
leave them as text until an INSERT happens.  The trick looks like this:
dt datetime default text 'now'

or
dt datetime default 'now'::text

> i fixed problem in this way:
> create teble x(
> i int2,
> d date default date( now() ),                             // or now() ::
> date
> t time default datetime_time(datetime (now ()) ),
> dt datetime default now()
> );

That should work too, although it's pretty ugly for all except the
datetime case.  (Do you really need to write the conversion functions
explicitly?  At least in recent Postgres releases, I think they should
get added for you...)
        regards, tom lane


pgsql-sql by date:

Previous
From: "Tiberiu Craciun"
Date:
Subject: Re: [SQL] calculating percentages
Next
From: Tom Lane
Date:
Subject: Re: [SQL] calculating percentages