Oracle
 sql >> Database >  >> RDS >> Oracle

Divisione di stringhe in PL/SQL

Prova questa procedura!!

Funzionerà se il modello di descrizione sarà lo stesso con gli spazi specificati e il flusso come descrizione, numero di telefono, quantità

create or replace procedure sp_split_str
as
     end_pos number:=0;
     strt_pos number :=1;
     CNT NUMBER:=0;
     v_desc varchar(200);
begin
     for i in (select * from table) loop
            if(instr(i.description,':',1,1)=0) then
                   dbms_output.put_line('Server Name : '||i.servername);
                   dbms_output.put_line('Description : '||substr(i.description,1,instr(i.description,'#',1,1)-2));
                   dbms_output.put_line('Phone Number : '||substr(i.description,instr(i.description,'#',1,1)+1,instr(i.description,'Q',-1,1)-2-instr(i.description,'#',1,1)));
                   dbms_output.put_line('Qty : '||substr(i.description,instr(i.description,'Q',-1,1)+4));           
                   dbms_output.put_line('Object State : '||i.objectstate);
            else
                   for J in 1..(length(i.description)-length(replace(i.description,':','')))+1 loop
                          IF(J=1) THEN
                                 end_pos := instr(i.description,':',1,1);
                                 v_desc := substr(i.description,strt_pos,end_pos-2);
                          ELSE
                                 end_pos := instr(i.description,':',1,j);
                                 strt_pos := instr(i.description,':',1,j-1);
                          END IF;
                          dbms_output.put_line('Server Name : '||i.servername);
                          dbms_output.put_line('Description : '||substr(v_desc,1,instr(v_desc,'#',1,1)-2));
                          dbms_output.put_line('Phone Number : '||substr(v_desc,instr(v_desc,'#',1,1)+1,instr(v_desc,'Q',-1,1)-2-instr(v_desc,'#',1,1)));
                          dbms_output.put_line('Qty : '||substr(v_desc,instr(v_desc,'Q',-1,1)+4));          
                          dbms_output.put_line('Object State : '||i.objectstate);

                          CNT := CNT+1;
                          IF(CNT=J) THEN
                                  v_desc := substr(i.description,strt_pos+2);
                          ELSE
                                  v_desc := substr(i.description,strt_pos+2,end_pos-2);
                          END IF;
                   END loop;
            end if;
      END LOOP;

END;