Обсуждение: PSQL

Поиск
Список
Период
Сортировка

PSQL

От
Atif Jung
Дата:
I'm having problems with the following stored procedure. The problematic line is the one in red. Any help greatly appreciated.
 
CREATE or replace FUNCTION converttojulian(datex DATE) RETURNS CHAR(5) AS $$
   DECLARE
   result  CHAR(5);
   m1  INTEGER;
   d1  INTEGER;
   y1  INTEGER;
   year  CHAR(4);
   d2  INTEGER;
   days  CHAR(3);
   days1  CHAR(3);
   date2  DATE;
BEGIN
   m1 := EXTRACT (MONTH FROM DATE datex);
   d1 := EXTRACT (DAY FROM DATE datex);
   y1 := EXTRACT (YEAR FROM DATE datex);
   date2 := MDY(m1,d1,y1);
   d2 := CAST((datex -date2)+1 AS INTEGER);
   year := SUBSTR(CAST(y1 AS CHAR(4)),1,4);
   days1 := SUBSTR(CAST(d2 AS CHAR(3)),1,3);
   IF d2 < 10 THEN
      days := '00' || SUBSTR(days1,1,1);
   ELSEIF d2 < 100 THEN
      days := '0' || SUBSTR(days1,1,2);
   ELSE
      days := SUBSTR(days1,1,3);
   END IF;
   result := SUBSTR(year,3,2) || days;
   RETURN result;
END;
$$ LANGUAGE plpgsql;
 
The error reads :
 
psql:converttojulian.sql:32: ERROR:  syntax error at or near "$1"
LINE 1: SELECT  EXTRACT (MONTH FROM DATE  $1 )
                                                                            ^

QUERY:  SELECT  EXTRACT (MONTH FROM DATE  $1 )
CONTEXT:  SQL statement in PL/PgSQL function "converttojulian" near line 12
 
It doesn't seem to like the use of the input paramater datex. I've tried using an alias but still get the same problem.
 
Thanks

Atif

Re: PSQL

От
Tom Lane
Дата:
Atif Jung <atifjung@gmail.com> writes:
> I'm having problems with the following stored procedure. The problematic
> line is the one in red. Any help greatly appreciated.

> CREATE or replace FUNCTION converttojulian(datex DATE) RETURNS CHAR(5) AS $$
> ...
>    m1 := EXTRACT (MONTH FROM DATE datex);

Drop the word DATE here.  datex is already a date, so you don't need to
cast it, and if you did need to cast it this would be the wrong syntax
anyway.  DATE 'foo' is only appropriate syntax for a simple literal
constant 'foo'.

            regards, tom lane