Postgres function syntax error

by SREENATH PILLAI   Last Updated October 14, 2018 05:06 AM

This is the body of a function. I keep getting a syntax error "near CAST" near the first CAST statement. Note: i intend to CAST all the trim results, but that's not the current issue.

-- logic to convert 53N 1E 8 to 4530108
DECLARE 
    dir integer;
"twn" ALIAS FOR $1;
"range" ALIAS FOR $2;
"section" ALIAS FOR $3;
BEGIN
  IF "range" LIKE '%E' THEN
    SET dir=4; 
    SET "range"=CAST(trim(both 'E' from "range") as varchar(5));
  ELSE
    SET dir=2; 
    SET "range"=trim(both 'W' from "range");
  END IF
  SET twn = trim(both 'N' from twn);
  IF CAST("range" AS integer) < 10
    SET "range" = '0' || "range"
  END IF
  IF CAST("section" AS integer) < 10
    SET "section" = '0' || "section"
  END IF
  IF CAST(twn AS integer) < 10
    SET twn = '0' || twn
  END IF
  RETURN dir || twn || "range" || "section";
END;


Related Questions




Efficiency of Scalar UDF vs TVF

Updated July 30, 2018 22:06 PM

Array of template type in PL/pgSQL function using %TYPE

Updated February 25, 2016 01:02 AM