Re: Casting varchar to timestamp fails in plpgsql

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Bart Teeuwisse <bart-postgres(at)7-sisters(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Casting varchar to timestamp fails in plpgsql
Date: 2002-02-25 16:19:36
Message-ID: 21386.1014653976@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Bart Teeuwisse <bart-postgres(at)7-sisters(dot)com> writes:
> While the following cast works in psql, an equivalent cast fails in plpgsql:

These are not equivalent.

> select cast ('Dec 14 1901 08:45:52' as timestamp);

What you have here is not a run-time cast, but simply assignment of a
datatype to a literal of previously unspecified type. The CAST notation
can mean either that or a run-time cast depending on what you apply it to.

> The following equivalent plgsql function
> create function rdbms_date(varchar) returns timestamp as '
> declare
> p_raw_date alias for $1;
> begin
> return cast (p_raw_date as timestamp);
> end;' language 'plpgsql';
> fails with error message:
> ERROR: Cannot cast type 'varchar' to 'timestamp'

What would actually be equivalent is

test71=# select cast ('Dec 14 1901 08:45:52'::varchar as timestamp);
ERROR: Cannot cast type 'varchar' to 'timestamp'

However, there is a text-to-timestamp converter, so this works:

test71=# select cast ('Dec 14 1901 08:45:52'::text as timestamp);
?column?
------------------------
1901-12-14 08:45:52-05
(1 row)

So you could cast the varchar argument to text first (or more likely,
declare it as text to begin with). Or you could simply omit the CAST
and allow default type conversion to occur. plpgsql would be perfectly
happy to cast varchar to text and thence to timestamp for you --- but
when you put in an explicit CAST, the system assumes that you are trying
to specify an exact type conversion path, and it won't help you out by
silently adding additional conversions (varchar->text in this case).

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Stephan Szabo 2002-02-25 16:20:30 Re: Work Around For Oracle Feature
Previous Message Thomas T. Thai 2002-02-25 16:15:34 Re: help with getting index scan