Re: Equivalent of Reverse() functions - Mailing list pgsql-sql

From greg@turnstep.com
Subject Re: Equivalent of Reverse() functions
Date
Msg-id c868f12fac1afa8fcf1a4e25eb33e81d@biglumber.com
Whole thread Raw
In response to Equivalent of Reverse() functions  ("Kumar" <sgnerd@yahoo.com.sg>)
List 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;
SELECTsubstring(original FROM mypos FOR 1) INTO onechar;     reversed := reversed || onechar;     mypos := mypos -1;
ENDLOOP;   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@turnstep.com
PGP Key: 0x14964AC8 200311291246
-----BEGIN PGP SIGNATURE-----
iD8DBQE/yNwwvJuQZxSWSsgRAnTyAJ9TqV0D3pV4Cv2b0VZfb8TxuvgxKgCeNBN+
OoFWwoD3omlLw+MUxcWZkT0=
=JtRf
-----END PGP SIGNATURE-----




pgsql-sql by date:

Previous
From: Stephan Szabo
Date:
Subject: Re: Seq Scans when index expected to be used
Next
From: ow
Date:
Subject: Re: Seq Scans when index expected to be used