CREATE OR REPLACE FUNCTION FORMATPRATMASK (P_CDPRATO IN VARCHAR2) RETURN VARCHAR2
IS
   V_FORMATMASK VARCHAR2(50);
   V_CDPICTPRAT VARCHAR2(50);
   V_CDPRATO  VARCHAR2(50);
   V_MASKLENGTH FLOAT;
BEGIN
   SELECT CDPICTPRAT, LENGTH(CDPICTPRAT)
     INTO V_CDPICTPRAT, V_MASKLENGTH
     FROM PARAMGERAL;
     
   V_CDPRATO := P_CDPRATO;
   FOR V_LOOP IN 1..V_MASKLENGTH
   LOOP
      IF(SUBSTR(V_CDPICTPRAT, V_LOOP, 1) = '.') THEN
         V_FORMATMASK := V_FORMATMASK||'.';
         V_CDPRATO  := V_FORMATMASK||SUBSTR(V_CDPRATO, V_LOOP, V_MASKLENGTH);
      ELSE
         V_FORMATMASK := V_FORMATMASK||SUBSTR(V_CDPRATO, V_LOOP, 1);
      END IF;
   END LOOP;
   
   WHILE SUBSTR(V_FORMATMASK, LENGTH(V_FORMATMASK), 1) = '.'
   LOOP
      V_FORMATMASK := SUBSTR(V_FORMATMASK, 0 , LENGTH(V_FORMATMASK)-1);
   END LOOP;
   RETURN(V_FORMATMASK);
END;
/