Re: Equivalent of Reverse() functions

From: greg(at)turnstep(dot)com
To: pgsql-sql(at)postgresql(dot)org
Cc: sgnerd(at)yahoo(dot)com(dot)sg
Subject: Re: Equivalent of Reverse() functions
Date: 2003-11-29 17:51:10
Message-ID: c868f12fac1afa8fcf1a4e25eb33e81d@biglumber.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


> I am migrating an SQL Server 2000 database to Postgres 7.3.4 running
> on RH Linux 7.2. While migrating I encounter SQL Server REVERSE( )
> function, seems no such functions at Postgres.
>
> Is there a equivalent function available at Postgres? Please
> shed some light

No. If you have plperl installed, as somebody already posted, you can
take advantage of perl's built in reverse() function. Here is a
plpgsql version:

CREATE OR REPLACE FUNCTION reverse(TEXT) RETURNS TEXT AS '
DECLARE
original ALIAS FOR $1;
reversed TEXT := \'\';
onechar VARCHAR;
mypos INTEGER;
BEGIN
SELECT LENGTH(original) INTO mypos;
LOOP
EXIT WHEN mypos < 1;
SELECT substring(original FROM mypos FOR 1) INTO onechar;
reversed := reversed || onechar;
mypos := mypos -1;
END LOOP;
RETURN reversed;
END
' LANGUAGE plpgsql IMMUTABLE RETURNS NULL ON NULL INPUT;

pg=> SELECT reverse('A man, a plan, a canal, Panama');

reverse
- --------------------------------
amanap ,lanac a ,nalp a ,nam A


- --
Greg Sabino Mullane greg(at)turnstep(dot)com
PGP Key: 0x14964AC8 200311291246
-----BEGIN PGP SIGNATURE-----

iD8DBQE/yNwwvJuQZxSWSsgRAnTyAJ9TqV0D3pV4Cv2b0VZfb8TxuvgxKgCeNBN+
OoFWwoD3omlLw+MUxcWZkT0=
=JtRf
-----END PGP SIGNATURE-----

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Andreas Heissenberger 2003-11-29 17:58:29
Previous Message Stephan Szabo 2003-11-29 17:37:12 Re: Seq Scans when index expected to be used