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;
/

ebrian
06-11-2010, 08:40 PM
SQL> select * from hosts;

HOSTID
-----------------------------------------------------------------------------
host1:12.134.23.45,host2:34.56.76.102

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.

ie., something like

host1
or

12.3.4.3

ebrian
06-18-2010, 12:41 AM
This should work:

SQL> select * from hosts;

HOSTID
----------------------------------------------------------------------
host1:12.134.23.45,host2:34.56.76.102,hosts3,hosts4:10.2.1.0,12.3.4.3,
hosts5.local:198.162.10.1,hosts6.local.com


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;

HOSTNAME IP
-------------------- --------------------
host1 12.134.23.45
host2 34.56.76.102
hosts3
hosts4 10.2.1.0
12.3.4.3
hosts5.local 198.162.10.1
hosts6.local.com

7 rows selected.

Sonia
06-24-2010, 08:07 AM
Ebrain,

Thx. It works like a charm.


sorry that i am asking you more.

The query is bit complicated, and whatever i try to moidfy, i am not able to.

Some fileds are coming like host1:12.2.3.4:303

iie., they are coming with port at the end.

I just want to ignore all the data after the second colon ie, igonore 303

Can u pls help me with this modification.

Thanks
sonia

ebrian
06-24-2010, 11:02 AM
SQL> select * from hosts;

HOSTID
--------------------------------------------------------------------------------
host1:12.134.23.45,host2:34.56.76.102:1521,hosts3,hosts4:10.2.1.0,12.3.4.3:3030,
hosts5.local:198.162.10.1:1521,hosts6.local.com,host7:3000

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;

HOSTNAME IP
-------------------- --------------------
host1 12.134.23.45
host2 34.56.76.102
hosts3
hosts4 10.2.1.0
12.3.4.3
hosts5.local 198.162.10.1
hosts6.local.com
host7

8 rows selected.

Sonia
06-29-2010, 05:28 PM
Thx ebrain.

Sonia
09-03-2010, 03:12 PM
This should work:

SQL> select * from hosts;

HOSTID
----------------------------------------------------------------------
host1:12.134.23.45,host2:34.56.76.102,hosts3,hosts4:10.2.1.0,12.3.4.3,
hosts5.local:198.162.10.1,hosts6.local.com


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;

HOSTNAME IP
-------------------- --------------------
host1 12.134.23.45
host2 34.56.76.102
hosts3
hosts4 10.2.1.0
12.3.4.3
hosts5.local 198.162.10.1
hosts6.local.com

7 rows selected.

Ebrain,

If somebody enters data like host1:IP12.2.22.1 the whole value is taken as host name.

ie anything other than numerical after : is taken as hostname. Is there a way to move it to ip address field .

Thanks
sonia

ebrian
09-04-2010, 09:50 PM
SQL> select * from hosts;

HOSTID
--------------------------------------------------------------------
host1:12.134.23.45,host2:IP34.56.76.102:1521,hosts3,hosts4:10.2.1.0,
12.3.4.3:3030,hosts5.local:198.162.10.1:1521,hosts6.local.com,host7:
3000


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;

HOSTNAME IP
-------------------- --------------------
host1 12.134.23.45
host2 34.56.76.102
hosts3
hosts4 10.2.1.0
12.3.4.3
hosts5.local 198.162.10.1
hosts6.local.com
host7

8 rows selected.

Sonia
09-07-2010, 08:24 AM
thx.