create or replace function replace (varchar, varchar, varchar) returns varchar as '
declare
    string      alias for $1;
    find        alias for $2;
    replacement alias for $3;

    match       integer;
    end_before  integer;
    start_after integer;

    string_replaced  varchar;
    string_remainder varchar;
begin
    string_remainder := string;
    string_replaced  := '''';
    match            := position(find in string_remainder);

    while (match > 0) loop
      end_before  := match - 1;
      start_after := match + length(find);
      string_replaced  := string_replaced || substr(string_remainder, 1, end_before) || replacement;
      string_remainder := substr(string_remainder, start_after);
      match := position(find in string_remainder);
      raise notice ''replaced %, remainder %'', string_replaced, string_remainder;
    end loop;
    string_replaced := string_replaced || string_remainder;

    return string_replaced;
end;
' LANGUAGE 'plpgsql';
    
--  rmello@fslc.usu.edu
