plpgsqlpostgresql-14

syntax error when creating an if statement on postgresql function


I'm learning postgresql and am trying to create a function. I have code like this:

create or replace function test_function(current_uncertainty float, last_updated timestamp, min_uncertainty float)
   returns float
   language plpgsql
  as
$$
declare
   -- variable declaration
    default_uncertainty float;
    days_not_played float;
    one_month float;
begin
   -- logic
    default_uncertainty = 25/3;
    one_month = 365 / 12;
    days_not_played = abs(DATE_PART('day', (now()- last_updated )));

    if(days_not_played >= 365) then
        return default_uncertainty;
    elseif days_not_played <= one_month then
        return max(current_uncertainty, min_uncertainty);
    else
        -- TODO
         return 0;

    end if;
 end;
$$;

I am getting the following error:

SQL Error [42601]: ERROR: syntax error at or near "if"
  Position: 5

Error position: line: 25 pos: 4

Solution

  • please verify the syntax of your elseif it should be elsif .

    Please refer the syntax at postgresql official documentation :

    https://www.postgresql.org/docs/7.2/plpgsql-control-structures.html