我试图弄清楚如何反转结果的路径。假设结果路径是abc / def / ghi,所以我需要反转它,所以结果读作“ghi / def / abc”。
我搜索过这个,文森特(Reverse in Oracle this path z/y/x to x/y/z)有一个解决方案。
然而,当我尝试做同样的事情时,我得到一个错误“参数'0'超出范围”。
以下是我的所作所为:
SQL> variable path varchar2(4000); SQL> exec :path := 'a/b/c/def';
PL/SQL procedure successfully completed
path
a/b/c/def
SQL> SELECT regexp_substr(:path, '[^/]+', 1, ROWNUM) sub_path, ROWNUM rk 2 from dual 3 CONNECT BY LEVEL <= length(regexp_replace(:path, '[^/]', '')) + 1;
SUB_PATH RK
a 1 b 2 c 3 def 4
path
a/b/c/def
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;
ORA-01428: argument '0' is out of range
path
a/b/c/def
我试过但很难弄清楚这个错误的原因。一旦我能够理解,那么也许我可以使用它来进行查询以生成反转路径。仅供参考,REVERSE功能不起作用,因为它只能反转不是所需结果的字符。
非常感谢任何帮助或提示。非常感谢!!
答案 0 :(得分:0)
@Joe非常感谢!! SQL Fiddle帮助我找到答案,虽然我无法找到解决方案,但下面的查询提供了我需要的结果。
SELECT MAX(sys_connect_by_path(sub_path, '/')) reversed_path
FROM
(SELECT regexp_substr(:path, '[^/]+', 1, ROWNUM) sub_path,
ROWNUM rk FROM duh
CONNECT BY LEVEL <= length(regexp_replace(:path, '[^/]', '')) + 1)
CONNECT BY PRIOR rk = rk + 1
答案 1 :(得分:0)
@ user3761113
尝试此查询,
SELECT REGEXP_SUBSTR(PATH,'[^/]+',1,4) || '/' || REGEXP_SUBSTR(PATH,'[^/]+',1,3) || '/' || REGEXP_SUBSTR(PATH,'[^/]+',1,2) || '/' || REGEXP_SUBSTR(PATH,'[^/]+',1,1) "Reverse of Path"
FROM (SELECT 'a/bc/def/ghij' PATH FROM DUAL);
我会这样做: - )