Funzione SPLIT in PL-SQL
Riporto un sistema molto semplice per realizzare una funzione SPLIT in PL-SQL. Si tratta di una procedura che ho trovato in questo blog.
Si tratta di una funzione che, data una stringa con separatori, restityuisce l’elemento i-esimo.
create or replace function get_token( the_list varchar2, the_index number, delim varchar2:= ',' ) return varchar2 is start_pos number; end_pos number; begin if the_index = 1 then start_pos := 1; else start_pos := instr(the_list, delim, 1, the_index - 1); if start_pos = 0 then return null; else start_pos := start_pos + length(delim); end if; end if; end_pos := instr(the_list, delim, start_pos, 1); if end_pos = 0 then return substr(the_list, start_pos); else return substr(the_list, start_pos, end_pos - start_pos); end if; end get_token; /
Si tratta di una funzione molto semplice. Il risultato è il seguente:
select get_token('foo,bar,baz',1), -- 'foo' get_token('foo,bar,baz',3), -- 'baz' -- get_token('a,,b',2), -- '' (null) get_token('a,,b',3), -- 'b' -- get_token('a|b|c',2,'|'), -- 'b' get_token('a|b|c',4,'|') -- '' (null) from dual
Punti di attenzione
- L’indice parte da 1 e non da 0
- Se per l’indice specificato, non è presente alcun valore, allora non viene restituito alcun valore
- Se si fornisce un indice che non esiste (es. la stringa è formata da 3 elementi e si richiede il 4 elemento), allora anche in questo caso, la funzione non restituisce nulla.
Suggerimenti
Potrebbe tornare utile far restituire una stringa ad hoc, qualora non venga fornito un indice non esistente nella stringa, sostituendo il return null; con return ‘-1’; in modo da distinguere la situazione in cui non venga restituito un valore da l’aver richiesto un indice non esistente.
Likes
(0)Dislikes
(0)


Ti è piaciuto il post? Vuoi una consulenza sui prodotti Atlassian o sui servizi offerti da Artigiano Del Software?
Contattaci. Scrivi una mail al supporto Artigiano. Saremo ben lieti di rispondere e aiutarti nella consulenza.Creare una funzione split con plsql