Re: Request to add feature to the Position function

From: Mark Watson <mark(dot)watson(at)jurisconcept(dot)ca>
To: Ron Ben <ronb910(at)walla(dot)co(dot)il>, "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Request to add feature to the Position function
Date: 2017-03-27 17:31:40
Message-ID: 02F1BBB48ABD3245A3BC519B57CFC8CA013C3B414E@Exchange.JurisConcept.local
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

De : pgsql-general-owner(at)postgresql(dot)org [mailto:pgsql-general-owner(at)postgresql(dot)org] De la part de Ron Ben
Envoyé : Monday, March 27, 2017 11:05 AM
À : pgsql-general(at)postgresql(dot)org
Objet : [GENERAL] Request to add feature to the Position function
<clip>

> position(substring in string)

> as listed here:

> https://www.postgresql.org/docs/9.1/static/functions-string.html

> locates sub string in a string.

>

> It doesn't support locateing the substring from the back.
<clip>

If what you mean by ‘from the back’ is ‘the last occurrence in a string read from left to right’, here is a quickie plpgsql function:

CREATE OR REPLACE FUNCTION rposition(substr text, str text)
RETURNS integer AS
$BODY$
declare
pos integer;
lastpos integer;
begin
pos := position(substr in str);
lastpos := 0;

while pos <> 0 loop
lastpos := pos;
pos := position(substr in substring(str from pos + 1));
if pos > 0 then pos := pos + lastpos ; end if;
end loop;
return lastpos;
end;
$BODY$
LANGUAGE plpgsql IMMUTABLE

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Felix Kunde 2017-03-27 20:48:09 Re: Trigger based logging alternative to table_log
Previous Message Tom Lane 2017-03-27 17:27:46 Re: migration to 9.6 array_accum memory issues