-- -- instr/2 -- create or replace function instr( character varying, character ) returns int4 as $$ declare str alias for $1; pat alias for $2; begin return instr(str,pat,1,1); end;$$ language plpgsql; -- -- instr/3 -- create or replace function instr( character varying, character, integer ) returns int4 as $$ declare str alias for $1; pat alias for $2; dir alias for $3; begin return instr(str,pat,dir,1); end;$$ language plpgsql; -- -- instr/4 -- create or replace function instr( character varying, character, integer, integer ) returns int4 as $$ declare str alias for $1; pat alias for $2; dir alias for $3; cnt alias for $4; v_len integer; v_i integer; v_c char; v_cnt integer; v_inc integer; begin v_len := length(str); v_cnt := 0; if dir < 0 then v_inc := -1; v_i := v_len; else v_inc := 1; v_i := 1; end if; while v_i > 0 and v_i <= v_len LOOP v_c := substr(str,v_i,1); if v_c::char = pat::char then v_cnt := v_cnt + 1; if v_cnt = cnt then return v_i; end if; end if; v_i := v_i + v_inc; end loop; return 0; end;$$ language plpgsql;