CREATE OR REPLACE FUNCTION MAX_NIVEL(P_MASCARA VARCHAR2) RETURN VARCHAR2
AS   
   V_DIGITO VARCHAR2(1);   
   V_TAM_MASCARA NUMBER;
   V_NUM_NIVEIS NUMBER;
BEGIN
   V_NUM_NIVEIS := 0;   
   IF(P_MASCARA IS NOT NULL) THEN
      V_TAM_MASCARA := LENGTH(P_MASCARA);      
      V_DIGITO := '';
      FOR I IN 1..V_TAM_MASCARA LOOP         
         V_DIGITO := SUBSTR(P_MASCARA, I, 1);
         IF(V_DIGITO = '.') THEN            
            V_NUM_NIVEIS := V_NUM_NIVEIS + 1;
         ELSE
            IF I = V_TAM_MASCARA THEN 
               V_NUM_NIVEIS := V_NUM_NIVEIS + 1;
            END IF;
         END IF;
      END LOOP;      
   END IF;

   RETURN V_NUM_NIVEIS;
END;
/