Thread: create function problem

create function problem

From
"Thomas Leung"
Date:
I want to index my table using the year of the date. So I create the
following function.

create function to_year(date) returns varchar as '
declare
  str varchar;
  begin
    str := to_char($1, "YYYY");
    return str;
  end;
' language 'plpgsql' with (iscachable, isstrict);

But I face the problem as follow
select to_year(current_date);
WARNING:  Error occurred while executing PL/pgSQL functions to_year
WARNING:  line 4 at assignment
ERROR:  Attribute "YYYY" not found

Anybody can help? Thanks.

_________________________________________________________________
Linguaphone :  Learning English? Get Japanese lessons for FREE
http://go.msnserver.com/HK/30476.asp


Re: create function problem

From
Stephan Szabo
Date:
On Tue, 16 Mar 2004, Thomas Leung wrote:

> I want to index my table using the year of the date. So I create the
> following function.
>
> create function to_year(date) returns varchar as '
> declare
>   str varchar;
>   begin
>     str := to_char($1, "YYYY");
>     return str;
>   end;
> ' language 'plpgsql' with (iscachable, isstrict);
>
> But I face the problem as follow
> select to_year(current_date);
> WARNING:  Error occurred while executing PL/pgSQL functions to_year
> WARNING:  line 4 at assignment
> ERROR:  Attribute "YYYY" not found
>
> Anybody can help? Thanks.

I think you're going towant doubled single quotes (''YYYY'') not double
quotes.

Re: create function problem

From
Tom Lane
Date:
"Thomas Leung" <thomasleung@hotmail.com> writes:
>     str := to_char($1, "YYYY");

> ERROR:  Attribute "YYYY" not found

Double and single quotes are not interchangeable.  Here you have written
an identifier, not a string literal.  You'll need
     str := to_char($1, ''YYYY'');
or
     str := to_char($1, \'YYYY\');
according to taste.

            regards, tom lane