pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: greg@turnstep.com
To: pgsql-sql@postgresql.org
Cc: sgnerd@yahoo.com.sg
Subject: Re: Equivalent of Reverse() functions
Date: Sat, 29 Nov 2003 17:51:10 -0000
Message-ID: <c868f12fac1afa8fcf1a4e25eb33e81d@biglumber.com> (raw)
In-Reply-To: <00cb01c3b4b2$c23d9520$7502a8c0@hdsc.com>


-----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@turnstep.com
PGP Key: 0x14964AC8 200311291246
-----BEGIN PGP SIGNATURE-----
 
iD8DBQE/yNwwvJuQZxSWSsgRAnTyAJ9TqV0D3pV4Cv2b0VZfb8TxuvgxKgCeNBN+
OoFWwoD3omlLw+MUxcWZkT0=
=JtRf
-----END PGP SIGNATURE-----





view thread (3+ messages)

Message-ID: <c868f12fac1afa8fcf1a4e25eb33e81d@biglumber.com>
Permalink:  ../c868f12fac1afa8fcf1a4e25eb33e81d@biglumber.com/
Also on:    postgresql.org/message-id/c868f12fac1afa8fcf1a4e25eb33e81d@biglumber.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: greg@turnstep.com, sgnerd@yahoo.com.sg
  Subject: Re: Equivalent of Reverse() functions
  In-Reply-To: <c868f12fac1afa8fcf1a4e25eb33e81d@biglumber.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox