strpos(str, sub) Postgres instr(str, sub) Oracle.
, , Postgres .
substr(str, n) str, n.
instr(str, ch, instr(str, sub), 1); --oracle
strpos(substr(str, strpos(str, sub)), ch) + strpos(str, sub) - 1; --postgres
instr() , plpgsql .
create or replace function instr(str text, sub text, startpos int = 1, occurrence int = 1)
returns int language plpgsql
as $$
declare
tail text;
shift int;
pos int;
i int;
begin
shift:= 0;
if startpos = 0 or occurrence <= 0 then
return 0;
end if;
if startpos < 0 then
str:= reverse(str);
sub:= reverse(sub);
pos:= -startpos;
else
pos:= startpos;
end if;
for i in 1..occurrence loop
shift:= shift+ pos;
tail:= substr(str, shift);
pos:= strpos(tail, sub);
if pos = 0 then
return 0;
end if;
end loop;
if startpos > 0 then
return pos+ shift- 1;
else
return length(str)- pos- shift+ 1;
end if;
end $$;
( OLAP DML):
select instr('Corporate Floor', 'or', 3, 2);
select instr('Corporate Floor', 'or', -3, 2);
Postgres 8.2 reverse(). :
create or replace function reverse(str text)
returns text language plpgsql
as $$
declare
i int;
res text = '';
begin
for i in 1..length(str) loop
res:= substr(str, i, 1) || res;
end loop;
return res;
end $$;