si potrebbe usare ricorsiva factoring subquery (CTE ricorsiva):
with s (street_address, line, part_address, remaining) as (
select street_address, 0 as line,
null as part_address, street_address as remaining
from address
union all
select street_address, line + 1 as line,
case when length(remaining) <= 40 then remaining else
substr(remaining, 1, instr(substr(remaining, 1, 40), ' ', -1, 1)) end
as part_address,
case when length(remaining) <= 40 then null else
substr(remaining, instr(substr(remaining, 1, 40), ' ', -1, 1) + 1) end
as remaining
from s
)
cycle remaining set is_cycle to 'Y' default 'N'
select line, part_address
from s
where part_address is not null
order by street_address, line;
Quale spirito i tuoi dati:
LINE PART_ADDRESS
---------- ----------------------------------------
1 152 Main st North Pole Factory 44, near
2 the rear entrance cross the street and
3 turn left and keep walking straight.
SQL Fiddle demo con due indirizzi.
Puoi anche convertire quei valori parziali in colonne, che penso sia il tuo obiettivo finale, ad es. come vista:
create or replace view v_address as
with cte (street_address, line, part_address, remaining) as (
select street_address, 0 as line,
null as part_address, street_address as remaining
from address
union all
select street_address, line + 1 as line,
case when length(remaining) <= 40 then remaining else
substr(remaining, 1, instr(substr(remaining, 1, 40), ' ', -1, 1)) end
as part_address,
case when length(remaining) <= 40 then null else
substr(remaining, instr(substr(remaining, 1, 40), ' ', -1, 1) + 1) end
as remaining
from cte
)
cycle remaining set is_cycle to 'Y' default 'N'
select street_address,
cast (max(case when line = 1 then part_address end) as varchar2(40))
as address_1,
cast (max(case when line = 2 then part_address end) as varchar2(40))
as address_2,
cast (max(case when line = 3 then part_address end) as varchar2(40))
as address_3
from cte
where part_address is not null
group by street_address;
Another SQL Fiddle.
Vale la pena notare che se la lunghezza dello street_address
si avvicina a 120 caratteri, potrebbe non adattarsi perfettamente a 3 blocchi di 40 caratteri: si perdono alcuni caratteri a seconda della lunghezza della parola racchiusa nella "linea" successiva. . Questo approccio genererà più di 3 linee, ma la vista utilizza solo i primi tre, quindi potresti perdere la fine dell'indirizzo. Potresti voler rendere i campi più lunghi o avere un address_4
per quelle situazioni ...
Dove mostrerai queste colonne? Perché non lo dividi a livello di applicazione? – bjan