Click to See Complete Forum and Search --> : query help
Sonia
06-11-2010, 06:42 AM
hi,
I am trying to modify a query ( which i got from internet searching) to my requirement.
The below query works fine for string '1:3,4:6,8:10,3:4,7:6,11:12
but i am looking for a string of this format,
host1:12.134.23.45,host2:34.56.76.102,..................
I am not familiar with regexp_substr. But still i am working on this.
If somebody can help me, i would greatly appreciate that.
Thanks
sonia
---------------------------------------------
declare
z varchar2(40);
cursor get_vals is
with t as (
select '1:3,4:6,8:10,3:4,7:6,11:12' z from dual
)
select
rtrim(regexp_substr(z,'\d+:',1,level),':') first,
ltrim(regexp_substr(z,':\d+',1,level),':') second
from t
connect by level<=length(z)-length(replace(z,',')) + 1;
begin
for a in get_vals loop
insert into mytable
values (a.first, a.second);
end loop;
commit;
end;
/
SQL> with t as (
2 select ltrim(regexp_substr(hostid, '[^,]+', 1, level), ',') str
3 from hosts
4 connect by level <= length(hostid)-length(replace(hostid,':')))
5 select regexp_substr(str, '[^:]+') hostname
6 , regexp_substr(str, '[^:]+$') ip
7 from t;
HOSTNAME IP
---------- -------------------
host1 12.134.23.45
host2 34.56.76.102
Sonia
06-15-2010, 03:07 AM
amazing.
thx ebrain.
Sonia
06-15-2010, 09:07 AM
ebrain,
is there any way we can handle if the host name is null or ip address is null without a colon.
SQL> with t as (
2 select regexp_substr(hostid, '[^,]+', 1, level) str
3 from hosts
4 connect by level <= length(hostid)-length(replace(hostid,','))+1)
5 select regexp_substr(str, '.*[a-z]+[^:]*',1,1,'i') hostname
6 , ltrim(regexp_substr(str, '(^|:)[^:a-z]+$',1,1,'i'),':') ip
7 from t;
SQL> with t as (
2 select regexp_substr(hostid, '[^,]+', 1, level) str
3 from hosts
4 connect by level <= length(hostid)-length(replace(hostid,','))+1)
5 select regexp_substr(str, '.*[a-z]+[^:]*',1,1,'i') hostname
6 , ltrim(regexp_substr(str, '(^|:)[0-9]+[.][^:]+'),':') ip
7 from t;
SQL> with t as (
2 select regexp_substr(hostid, '[^,]+', 1, level) str
3 from hosts
4 connect by level <= length(hostid)-length(replace(hostid,','))+1)
5 select regexp_substr(str, '.*[a-z]+[^:]*',1,1,'i') hostname
6 , ltrim(regexp_substr(str, '(^|:)[^:a-z]+$',1,1,'i'),':') ip
7 from t;
SQL> with t as (
2 select regexp_substr(hostid, '[^,]+', 1, level) str
3 from hosts
4 connect by level <= length(hostid)-length(replace(hostid,','))+1)
5 select regexp_substr(str, '[^:]*[a-z]+[^:]*',1,1,'i') hostname
6 , regexp_substr(str, '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}') ip
7 from t;