create or replace FUNCTION FN_SUBCATEGORIA_JERARQUIA (v_Arbol_Completo varchar2, v_Parte_Rama number) RETURN Varchar2 AS v_Retorno varchar2(2000) := ''; v_Arbol_Completo2 varchar2(2000) := ''; v_Ramas number := 0; v_PrimeraPos number := 0; v_SegundaPos number := 0; v_MismaPos number := 0; BEGIN v_Arbol_Completo2 := v_Arbol_Completo; if v_Parte_Rama > 0 and length(ltrim(rtrim(v_Arbol_Completo2))) > 0 then Begin v_Ramas := Case v_Parte_Rama When 1 then 0 else 1 End; While v_Ramas <= (v_Parte_Rama - 1) loop v_PrimeraPos := Case instr(v_Arbol_Completo2, '.') When 0 Then 1 Else (instr(v_Arbol_Completo2, '.') + 1) End; if v_Parte_Rama = 1 then Begin v_Retorno := Substr(v_Arbol_Completo2, 1, Case v_PrimeraPos When 1 then length(v_Arbol_Completo2) Else (v_PrimeraPos - 2) End); exit; End; End if; v_SegundaPos := Case instr(Substr(v_Arbol_Completo2, v_PrimeraPos, length(v_Arbol_Completo2)), '.') When 0 Then length(v_Arbol_Completo2) Else (instr(Substr(v_Arbol_Completo2, v_PrimeraPos, length(v_Arbol_Completo2)), '.') - 1) End; v_Retorno := Substr(v_Arbol_Completo2, v_PrimeraPos, v_SegundaPos); if v_Retorno = v_Arbol_Completo2 then Begin v_Retorno := ''; exit; End; End if; v_Arbol_Completo2 := Substr(v_Arbol_Completo2, v_PrimeraPos, length(v_Arbol_Completo2)); v_Ramas := v_Ramas + 1; End loop; End; Else v_Retorno := ''; End if; RETURN (v_Retorno); end FN_SUBCATEGORIA_JERARQUIA;