DROP FUNCTION QUEBRA_TEXTO
/
DROP TYPE TEXTOQUEBRADO_TABLE
/
DROP TYPE TEXTOQUEBRADO_TYPE
/

create or replace TYPE TEXTOQUEBRADO_TYPE AS
OBJECT (DESCTEXTO1      VARCHAR2(15),
        DESCTEXTO2      VARCHAR2(15),
        DESCTEXTO3      VARCHAR2(15),
        DESCTEXTO4      VARCHAR2(15),
        DESCTEXTO5      VARCHAR2(15),
        DESCTEXTO6      VARCHAR2(15),
        DESCTEXTO7      VARCHAR2(15),
        DESCTEXTO8      VARCHAR2(15),
        DESCTEXTO9      VARCHAR2(15),
        DESCTEXTO10     VARCHAR2(15));

/

CREATE TYPE TEXTOQUEBRADO_TABLE AS
TABLE OF TEXTOQUEBRADO_TYPE
/

create or replace FUNCTION QUEBRA_TEXTO( 
   P_TEXTO     IN VARCHAR2, 
   P_QUEBRA    IN INTEGER) RETURN TEXTOQUEBRADO_TABLE PIPELINED 
IS 
 
   V_POSESPACO   INTEGER; 
   V_TXTQUEBRA   VARCHAR2(5000); 
   V_TXTRESTO    VARCHAR2(5000); 
   V_TXTRETORNO1 VARCHAR2(5000); 
   V_TXTRETORNO2 VARCHAR2(5000); 
   V_TXTRETORNO3 VARCHAR2(5000); 
   V_TXTRETORNO4 VARCHAR2(5000); 
   V_TXTRETORNO5 VARCHAR2(5000); 
   V_TXTRETORNO6 VARCHAR2(5000); 
   V_TXTRETORNO7 VARCHAR2(5000); 
   V_TXTRETORNO8 VARCHAR2(5000); 
   V_TXTRETORNO9 VARCHAR2(5000); 
   V_TXTRETORNO10 VARCHAR2(5000); 
   V_COUNT       INTEGER; 

OUT_RECORD TEXTOQUEBRADO_TYPE := TEXTOQUEBRADO_TYPE (NULL, NULL, NULL, NULL, NULL,
                                                     NULL, NULL, NULL, NULL, NULL); 

BEGIN 

   V_TXTQUEBRA  := ''; 
   V_TXTRESTO   := P_TEXTO; 

   V_POSESPACO := INSTR(P_TEXTO, ' '); 
   IF V_POSESPACO = 0 THEN 
      V_POSESPACO := LENGTH(P_TEXTO); 
   END IF; 
   V_TXTQUEBRA := V_TXTQUEBRA||SUBSTR(P_TEXTO, 1, V_POSESPACO); 
   V_TXTRESTO  := SUBSTR(P_TEXTO, (V_POSESPACO+1), LENGTH(P_TEXTO)); 

   V_TXTRETORNO1 := V_TXTQUEBRA; 
   V_COUNT := 0; 

   WHILE ((((LENGTH(V_TXTQUEBRA)) <= 15)) AND 
          (LENGTH(V_TXTRESTO) > 0)) LOOP 
      V_POSESPACO := INSTR(V_TXTRESTO, ' '); 
      IF V_POSESPACO = 0 THEN 
         V_POSESPACO := LENGTH(V_TXTRESTO); 
      END IF; 

      V_TXTRETORNO1 := V_TXTQUEBRA; 
      V_TXTQUEBRA := V_TXTQUEBRA||SUBSTR(V_TXTRESTO, 1, V_POSESPACO); 
      V_TXTRESTO  := SUBSTR(V_TXTRESTO, (V_POSESPACO+1), LENGTH(V_TXTRESTO)); 
      IF (LENGTH(V_TXTQUEBRA) <= 15) THEN 
         V_TXTRETORNO1 := V_TXTQUEBRA; 
      END IF; 





   END LOOP; 

   V_TXTQUEBRA  := ''; 
   V_TXTRESTO := LTRIM(RTRIM(SUBSTR(P_TEXTO,LENGTH(V_TXTRETORNO1)+1, LENGTH(P_TEXTO)))); 
   V_POSESPACO := INSTR(V_TXTRESTO, ' '); 
   IF V_POSESPACO = 0 THEN 
      V_POSESPACO := LENGTH(V_TXTRESTO); 
   END IF; 
   V_TXTQUEBRA := V_TXTQUEBRA||SUBSTR(V_TXTRESTO, 1, V_POSESPACO); 
   V_TXTRESTO  := SUBSTR(V_TXTRESTO, (V_POSESPACO+1), LENGTH(V_TXTRESTO)); 
   V_TXTRETORNO2 := V_TXTQUEBRA; 
   V_COUNT := 0; 

   WHILE ((((LENGTH(V_TXTQUEBRA)) <= 15)) AND 
          (LENGTH(V_TXTRESTO) > 0)) LOOP 

      V_POSESPACO := INSTR(V_TXTRESTO, ' '); 



      IF V_POSESPACO = 0 THEN 
         V_POSESPACO := LENGTH(V_TXTRESTO); 
      END IF; 

      V_TXTRETORNO2 := V_TXTQUEBRA; 
      V_TXTQUEBRA := V_TXTQUEBRA||SUBSTR(V_TXTRESTO, 1, V_POSESPACO); 

      V_TXTRESTO  := SUBSTR(V_TXTRESTO, (V_POSESPACO+1), LENGTH(V_TXTRESTO)); 

      IF (LENGTH(V_TXTQUEBRA) <= 15) THEN 
         V_TXTRETORNO2 := V_TXTQUEBRA; 
      END IF; 

      V_COUNT := V_COUNT +1; 



   END LOOP; 

   V_TXTQUEBRA  := ''; 
   V_TXTRESTO := SUBSTR(P_TEXTO,LENGTH(V_TXTRETORNO1||V_TXTRETORNO2)+1, LENGTH(P_TEXTO)); 

   V_POSESPACO := INSTR(V_TXTRESTO, ' '); 
   IF V_POSESPACO = 0 THEN 
      V_POSESPACO := LENGTH(V_TXTRESTO); 
   END IF; 
   V_TXTQUEBRA := V_TXTQUEBRA||SUBSTR(V_TXTRESTO, 1, V_POSESPACO); 
   V_TXTRESTO  := SUBSTR(V_TXTRESTO, (V_POSESPACO+1), LENGTH(V_TXTRESTO)); 
   V_TXTRETORNO3 := V_TXTQUEBRA; 
   V_COUNT := 0; 

   WHILE ((((LENGTH(V_TXTQUEBRA)) <= 15)) AND 
          (LENGTH(V_TXTRESTO) > 0)) LOOP 
      V_POSESPACO := INSTR(V_TXTRESTO, ' '); 
      IF V_POSESPACO = 0 THEN 
         V_POSESPACO := LENGTH(V_TXTRESTO); 
      END IF; 

      V_TXTRETORNO3 := V_TXTQUEBRA; 
      V_TXTQUEBRA := V_TXTQUEBRA||SUBSTR(V_TXTRESTO, 1, V_POSESPACO); 
      V_TXTRESTO  := SUBSTR(V_TXTRESTO, (V_POSESPACO+1), LENGTH(V_TXTQUEBRA)); 
      IF (LENGTH(V_TXTQUEBRA) <= 15) THEN 
         V_TXTRETORNO3 := V_TXTQUEBRA; 
      END IF; 





   END LOOP; 

   V_TXTQUEBRA  := ''; 
   V_TXTRESTO := SUBSTR(P_TEXTO,LENGTH(V_TXTRETORNO1||V_TXTRETORNO2||V_TXTRETORNO3)+1, LENGTH(P_TEXTO)); 
   V_POSESPACO := INSTR(V_TXTRESTO, ' '); 
   IF V_POSESPACO = 0 THEN 
      V_POSESPACO := LENGTH(V_TXTRESTO); 
   END IF; 
   V_TXTQUEBRA := V_TXTQUEBRA||SUBSTR(V_TXTRESTO, 1, V_POSESPACO); 
   V_TXTRESTO  := SUBSTR(V_TXTRESTO, (V_POSESPACO+1), LENGTH(V_TXTRESTO)); 
   V_TXTRETORNO4 := V_TXTQUEBRA; 
   V_COUNT := 0; 

   WHILE ((((LENGTH(V_TXTQUEBRA)) <= 15)) AND 
          (LENGTH(V_TXTRESTO) > 0)) LOOP 
      V_POSESPACO := INSTR(V_TXTRESTO, ' '); 
      IF V_POSESPACO = 0 THEN 
         V_POSESPACO := LENGTH(V_TXTRESTO); 
      END IF; 

      V_TXTRETORNO4 := V_TXTQUEBRA; 
      V_TXTQUEBRA := V_TXTQUEBRA||SUBSTR(V_TXTRESTO, 1, V_POSESPACO); 
      V_TXTRESTO  := SUBSTR(V_TXTRESTO, (V_POSESPACO+1), LENGTH(V_TXTQUEBRA)); 
      IF (LENGTH(V_TXTQUEBRA) <= 15) THEN 
         V_TXTRETORNO4 := V_TXTQUEBRA; 
      END IF; 





   END LOOP; 

   V_TXTQUEBRA  := ''; 
   V_TXTRESTO := SUBSTR(P_TEXTO,LENGTH(V_TXTRETORNO1||V_TXTRETORNO2||V_TXTRETORNO3||V_TXTRETORNO4)+1, LENGTH(P_TEXTO)); 
   V_POSESPACO := INSTR(V_TXTRESTO, ' '); 
   IF V_POSESPACO = 0 THEN 
      V_POSESPACO := LENGTH(V_TXTRESTO); 
   END IF; 
   V_TXTQUEBRA := V_TXTQUEBRA||SUBSTR(V_TXTRESTO, 1, V_POSESPACO); 
   V_TXTRESTO  := SUBSTR(V_TXTRESTO, (V_POSESPACO+1), LENGTH(V_TXTRESTO)); 
   V_TXTRETORNO5 := V_TXTQUEBRA; 
   V_COUNT := 0; 

   WHILE ((((LENGTH(V_TXTQUEBRA)) <= 15)) AND 
          (LENGTH(V_TXTRESTO) > 0)) LOOP 
      V_POSESPACO := INSTR(V_TXTRESTO, ' '); 
      IF V_POSESPACO = 0 THEN 
         V_POSESPACO := LENGTH(V_TXTRESTO); 
      END IF; 

      V_TXTRETORNO5 := V_TXTQUEBRA; 
      V_TXTQUEBRA := V_TXTQUEBRA||SUBSTR(V_TXTRESTO, 1, V_POSESPACO); 
      V_TXTRESTO  := SUBSTR(V_TXTRESTO, (V_POSESPACO+1), LENGTH(V_TXTQUEBRA)); 
      IF (LENGTH(V_TXTQUEBRA) <= 15) THEN 
         V_TXTRETORNO5 := V_TXTQUEBRA; 
      END IF; 





   END LOOP; 

   V_TXTQUEBRA  := ''; 
   V_TXTRESTO := SUBSTR(P_TEXTO,LENGTH(V_TXTRETORNO1||V_TXTRETORNO2||V_TXTRETORNO3||V_TXTRETORNO4||V_TXTRETORNO5)+1, LENGTH(P_TEXTO)); 
   V_POSESPACO := INSTR(V_TXTRESTO, ' '); 
   IF V_POSESPACO = 0 THEN 
      V_POSESPACO := LENGTH(V_TXTRESTO); 
   END IF; 
   V_TXTQUEBRA := V_TXTQUEBRA||SUBSTR(V_TXTRESTO, 1, V_POSESPACO); 
   V_TXTRESTO  := SUBSTR(V_TXTRESTO, (V_POSESPACO+1), LENGTH(V_TXTRESTO)); 
   V_TXTRETORNO6 := V_TXTQUEBRA; 
   V_COUNT := 0; 

   WHILE ((((LENGTH(V_TXTQUEBRA)) <= 15)) AND 
          (LENGTH(V_TXTRESTO) > 0)) LOOP 
      V_POSESPACO := INSTR(V_TXTRESTO, ' '); 
      IF V_POSESPACO = 0 THEN 
         V_POSESPACO := LENGTH(V_TXTRESTO); 
      END IF; 

      V_TXTRETORNO6 := V_TXTQUEBRA; 
      V_TXTQUEBRA := V_TXTQUEBRA||SUBSTR(V_TXTRESTO, 1, V_POSESPACO); 
      V_TXTRESTO  := SUBSTR(V_TXTRESTO, (V_POSESPACO+1), LENGTH(V_TXTQUEBRA)); 
      IF (LENGTH(V_TXTQUEBRA) <= 15) THEN 
         V_TXTRETORNO6 := V_TXTQUEBRA; 
      END IF; 





   END LOOP; 

   V_TXTQUEBRA  := ''; 
   V_TXTRESTO := SUBSTR(P_TEXTO,LENGTH(V_TXTRETORNO1||V_TXTRETORNO2||V_TXTRETORNO3||V_TXTRETORNO4||V_TXTRETORNO5||V_TXTRETORNO6)+1, LENGTH(P_TEXTO)); 
   V_POSESPACO := INSTR(V_TXTRESTO, ' '); 
   IF V_POSESPACO = 0 THEN 
      V_POSESPACO := LENGTH(V_TXTRESTO); 
   END IF; 
   V_TXTQUEBRA := V_TXTQUEBRA||SUBSTR(V_TXTRESTO, 1, V_POSESPACO); 
   V_TXTRESTO  := SUBSTR(V_TXTRESTO, (V_POSESPACO+1), LENGTH(V_TXTRESTO)); 
   V_TXTRETORNO7 := V_TXTQUEBRA; 
   V_COUNT := 0; 

   WHILE ((((LENGTH(V_TXTQUEBRA)) <= 15)) AND 
          (LENGTH(V_TXTRESTO) > 0)) LOOP 
      V_POSESPACO := INSTR(V_TXTRESTO, ' '); 
      IF V_POSESPACO = 0 THEN 
         V_POSESPACO := LENGTH(V_TXTRESTO); 
      END IF; 

      V_TXTRETORNO7 := V_TXTQUEBRA; 
      V_TXTQUEBRA := V_TXTQUEBRA||SUBSTR(V_TXTRESTO, 1, V_POSESPACO); 
      V_TXTRESTO  := SUBSTR(V_TXTRESTO, (V_POSESPACO+1), LENGTH(V_TXTQUEBRA)); 
      IF (LENGTH(V_TXTQUEBRA) <= 15) THEN 
         V_TXTRETORNO7 := V_TXTQUEBRA; 
      END IF; 





   END LOOP; 

   V_TXTQUEBRA  := ''; 
   V_TXTRESTO := SUBSTR(P_TEXTO,LENGTH(V_TXTRETORNO1||V_TXTRETORNO2||V_TXTRETORNO3||V_TXTRETORNO4||V_TXTRETORNO5||V_TXTRETORNO6||V_TXTRETORNO7)+1, LENGTH(P_TEXTO)); 
   V_POSESPACO := INSTR(V_TXTRESTO, ' '); 
   IF V_POSESPACO = 0 THEN 
      V_POSESPACO := LENGTH(V_TXTRESTO); 
   END IF; 
   V_TXTQUEBRA := V_TXTQUEBRA||SUBSTR(V_TXTRESTO, 1, V_POSESPACO); 
   V_TXTRESTO  := SUBSTR(V_TXTRESTO, (V_POSESPACO+1), LENGTH(V_TXTRESTO)); 
   V_TXTRETORNO8 := V_TXTQUEBRA; 
   V_COUNT := 0; 

   WHILE ((((LENGTH(V_TXTQUEBRA)) <= 15)) AND 
          (LENGTH(V_TXTRESTO) > 0)) LOOP 
      V_POSESPACO := INSTR(V_TXTRESTO, ' '); 
      IF V_POSESPACO = 0 THEN 
         V_POSESPACO := LENGTH(V_TXTRESTO); 
      END IF; 

      V_TXTRETORNO8 := V_TXTQUEBRA; 
      V_TXTQUEBRA := V_TXTQUEBRA||SUBSTR(V_TXTRESTO, 1, V_POSESPACO); 
      V_TXTRESTO  := SUBSTR(V_TXTRESTO, (V_POSESPACO+1), LENGTH(V_TXTQUEBRA)); 
      IF (LENGTH(V_TXTQUEBRA) <= 15) THEN 
         V_TXTRETORNO8 := V_TXTQUEBRA; 
      END IF; 





   END LOOP; 

   V_TXTQUEBRA  := ''; 
   V_TXTRESTO := SUBSTR(P_TEXTO,LENGTH(V_TXTRETORNO1||V_TXTRETORNO2||V_TXTRETORNO3||V_TXTRETORNO4||V_TXTRETORNO5||V_TXTRETORNO6||V_TXTRETORNO7||V_TXTRETORNO8)+1, LENGTH(P_TEXTO)); 
   V_POSESPACO := INSTR(V_TXTRESTO, ' '); 
   IF V_POSESPACO = 0 THEN 
      V_POSESPACO := LENGTH(V_TXTRESTO); 
   END IF; 
   V_TXTQUEBRA := V_TXTQUEBRA||SUBSTR(V_TXTRESTO, 1, V_POSESPACO); 
   V_TXTRESTO  := SUBSTR(V_TXTRESTO, (V_POSESPACO+1), LENGTH(V_TXTRESTO)); 
   V_TXTRETORNO9 := V_TXTQUEBRA; 
   V_COUNT := 0; 

   WHILE ((((LENGTH(V_TXTQUEBRA)) <= 15)) AND 
          (LENGTH(V_TXTRESTO) > 0)) LOOP 
      V_POSESPACO := INSTR(V_TXTRESTO, ' '); 
      IF V_POSESPACO = 0 THEN 
         V_POSESPACO := LENGTH(V_TXTRESTO); 
      END IF; 

      V_TXTRETORNO9 := V_TXTQUEBRA; 
      V_TXTQUEBRA := V_TXTQUEBRA||SUBSTR(V_TXTRESTO, 1, V_POSESPACO); 
      V_TXTRESTO  := SUBSTR(V_TXTRESTO, (V_POSESPACO+1), LENGTH(V_TXTQUEBRA)); 
      IF (LENGTH(V_TXTQUEBRA) <= 15) THEN 
         V_TXTRETORNO9 := V_TXTQUEBRA; 
      END IF; 
   END LOOP; 

   V_TXTQUEBRA  := ''; 
   V_TXTRESTO := SUBSTR(P_TEXTO,LENGTH(V_TXTRETORNO1||V_TXTRETORNO2||V_TXTRETORNO3||V_TXTRETORNO4||V_TXTRETORNO5||V_TXTRETORNO6||V_TXTRETORNO7||V_TXTRETORNO8||V_TXTRETORNO9)+1, LENGTH(P_TEXTO)); 
   V_POSESPACO := INSTR(V_TXTRESTO, ' '); 
   IF V_POSESPACO = 0 THEN 
      V_POSESPACO := LENGTH(V_TXTRESTO); 
   END IF; 
   V_TXTQUEBRA := V_TXTQUEBRA||SUBSTR(V_TXTRESTO, 1, V_POSESPACO); 
   V_TXTRESTO  := SUBSTR(V_TXTRESTO, (V_POSESPACO+1), LENGTH(V_TXTRESTO)); 
   V_TXTRETORNO10 := V_TXTQUEBRA; 
   V_COUNT := 0; 

   WHILE ((((LENGTH(V_TXTQUEBRA)) <= 15)) AND 
          (LENGTH(V_TXTRESTO) > 0)) LOOP 
      V_POSESPACO := INSTR(V_TXTRESTO, ' '); 
      IF V_POSESPACO = 0 THEN 
         V_POSESPACO := LENGTH(V_TXTRESTO); 
      END IF; 

      V_TXTRETORNO10 := V_TXTQUEBRA; 
      V_TXTQUEBRA := V_TXTQUEBRA||SUBSTR(V_TXTRESTO, 1, V_POSESPACO); 
      V_TXTRESTO  := SUBSTR(V_TXTRESTO, (V_POSESPACO+1), LENGTH(V_TXTQUEBRA)); 
      IF (LENGTH(V_TXTQUEBRA) <= 15) THEN 
         V_TXTRETORNO10 := V_TXTQUEBRA; 
      END IF; 
   END LOOP;

   IF V_TXTRETORNO1 IS NOT NULL THEN  
      OUT_RECORD.DESCTEXTO1 := NVL(V_TXTRETORNO1,' '); 
   END IF;    
   IF V_TXTRETORNO2 IS NOT NULL THEN 
      OUT_RECORD.DESCTEXTO2 := NVL(V_TXTRETORNO2,' '); 
   END IF;    
   IF V_TXTRETORNO3 IS NOT NULL THEN 
      OUT_RECORD.DESCTEXTO3 := NVL(V_TXTRETORNO3,' '); 
   END IF;    
   IF V_TXTRETORNO4 IS NOT NULL THEN  
      OUT_RECORD.DESCTEXTO4 := NVL(V_TXTRETORNO4,' '); 
   END IF;    
   IF V_TXTRETORNO5 IS NOT NULL THEN 
      OUT_RECORD.DESCTEXTO5 := NVL(V_TXTRETORNO5,' ');    
   END IF;    
   IF V_TXTRETORNO6 IS NOT NULL THEN 
      OUT_RECORD.DESCTEXTO6 := NVL(V_TXTRETORNO6,' '); 
   END IF;    
   IF V_TXTRETORNO7 IS NOT NULL THEN 
      OUT_RECORD.DESCTEXTO7 := NVL(V_TXTRETORNO7,' ');      
   END IF;    
   IF V_TXTRETORNO8 IS NOT NULL THEN 
      OUT_RECORD.DESCTEXTO8 := NVL(V_TXTRETORNO8,' '); 
   END IF;    
   IF V_TXTRETORNO9 IS NOT NULL THEN 
      OUT_RECORD.DESCTEXTO9 := NVL(V_TXTRETORNO9,' '); 
   END IF;    
   IF V_TXTRETORNO10 IS NOT NULL THEN 
      OUT_RECORD.DESCTEXTO10 := NVL(V_TXTRETORNO10,' '); 
   END IF; 
   PIPE ROW (OUT_RECORD); 

END;
/