create table strings as
select 'emcpower1' str from dual
union all
select 'emcpower2' from dual
union all
select 'emcpower3' from dual
union all
select 'sda' from dual
union all
select 'sdb' from dual
union all
select 'sdc' from dual
Table created.
with rws as (
-- generate rows up to the length of the longest string
select rownum r from dual
connect by level <= (select max(length(str)) from strings)
)
select distinct s
from (
select str, max(subp) s
from (
select str,
substr(str, 1, rws.r) subp
from strings s1
cross join rws
where r < length(str)
and exists (
-- check whether there's another string matching the first N chars
select * from strings s2
where s1.str <> s2.str
and substr(s1.str, 1, rws.r) = substr(s2.str, 1, rws.r)
)
)
group by str
)
S | emcpower | sd |
---|
insert into strings values ('emcdisk')
1 row(s) inserted.
insert into strings values ('samba')
1 row(s) inserted.
with rws as (
-- generate rows up to the length of the longest string
select rownum r from dual
connect by level <= (select max(length(str)) from strings)
)
select distinct s
from (
select str, max(subp) s
from (
select str,
substr(str, 1, rws.r) subp
from strings s1
cross join rws
where r < length(str)
and exists (
-- check whether there's another string matching the first N chars
select * from strings s2
where s1.str <> s2.str
and substr(s1.str, 1, rws.r) = substr(s2.str, 1, rws.r)
)
)
group by str
)
S | s | emcpower | emc | sd |
---|