Re: reverse strpos?

From: "A(dot) Kretschmer" <andreas(dot)kretschmer(at)schollglas(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: reverse strpos?
Date: 2007-11-12 17:01:09
Message-ID: 20071112170109.GA18644@a-kretschmer.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

am Mon, dem 12.11.2007, um 8:48:29 -0800 mailte David Fetter folgendes:
> > > Is there a function that?ll return the position of the last
> > > occurance of a char in a string?
> > >
> >
> > write a function to revert the string and use strpos().
> >
> > create or replace function rev(varchar) returns varchar as $$
> > declare
> > ...
> > $$ language plpgsql immutable;
> >
> >
> > Andreas
>
> PL/Perl might be easier:
>
> CREATE OR REPLACE FUNCTION rev(TEXT)
> RETURNS TEXT
> IMMUTABLE
> LANGUAGE plperl
> AS $$
> return reverse($_[0]);
> $$;

heh, the PERL-Guru ;-)

>
> CREATE OR REPLACE FUNCTION rev(TEXT)
> RETURNS TEXT
> IMMUTABLE
> LANGUAGE SQL
> AS $$
> SELECT array_to_string(
> ARRAY(
> SELECT substr($1,i,1)
> FROM generate_series(length($1),1,-1) AS i
> ),
> ''
> );
> $$;

Nice.

The generate_series()-function are really great.

Andreas
--
Andreas Kretschmer
Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID: 0x3FFF606C, privat 0x7F4584DA http://wwwkeys.de.pgp.net

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Greg Smith 2007-11-12 17:01:51 Re: Linux v.s. Mac OS-X Performance
Previous Message Scott Marlowe 2007-11-12 16:58:32 Re: update record with two-column primary key