tags:

views:

148

answers:

2

How would I do in a SELECT query to reverse this path :

z/y/x

for

x/y/z

where / is the delimiter and where there can be many delimiters in a single line

ex: select (... z/y/x/w/v/u ...) reversed_path from ...
+2  A: 

Are you looking for REVERSE?
i.e

SELECT REVERSE('z/y/x') FROM DUAL;
shahkalpesh
I looked it up - didn't know that REVERSE has been available since at least 8i.
OMG Ponies
thanks but no.
Jean-Philippe Martin
This is cool, but it works only for path elements with 1 character.
Christian13467
+3  A: 

The simplest way would probably be to write a stored pl/sql function, however it can be done with SQL (Oracle) alone.

This will decompose the path in subpath:

SQL> variable path varchar2(4000);
SQL> exec :path := 'a/b/c/def';

PL/SQL procedure successfully completed
SQL> SELECT regexp_substr(:path, '[^/]+', 1, ROWNUM) sub_path, ROWNUM rk
  2    FROM dual
  3  CONNECT BY LEVEL <= length(regexp_replace(:path, '[^/]', '')) + 1;

SUB_P RK
----- --
a      1
b      2
c      3
def    4

We then recompose the reversed path with the sys_connect_by_path:

SQL> SELECT MAX(sys_connect_by_path(sub_path, '/')) reversed_path
  2    FROM (SELECT regexp_substr(:path, '[^/]+', 1, ROWNUM) sub_path,
  3                 ROWNUM rk
  4             FROM dual
  5           CONNECT BY LEVEL <= length(regexp_replace(:path, '[^/]', '')) + 1)
  6  CONNECT BY PRIOR rk = rk + 1
  7   START WITH rk = length(regexp_replace(:path, '[^/]', '')) + 1;

REVERSED_PATH
-------------
/def/c/b/a
Vincent Malgrat
That'd only work in 11g+ due to the use of regex, correct?
OMG Ponies
REGEX was a new feature in 10g.
APC
Thank you so much !! Did not know that Oracle had regex. I'm with 10g so I'm ok. Stackoverflow is great ! :-) I will tear this apart to try to master it monday when I arrive at the office (don't have vpn here!).
Jean-Philippe Martin