Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Text Search containing IP address

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-07-08 : 08:41:36
Roger writes "I would like to search a text field (char,varchar ...) and select all the rows containing an IP address somewhere in the text field.

for example I would like to select a row contianing.

'123.1.2.3' as well as
'http://123.123.123.123/default.htm'

but I don't want to match '1.1234.2345.1234'

is this possible in SQL server?"

leedoolan
Starting Member

12 Posts

Posted - 2002-07-08 : 08:45:49
You need to use a like statement:

i.e. select *
from tablename
where colname like '%123.123.123%' or colname like '%123etc%'

hope this helps

Go to Top of Page

YellowBug
Aged Yak Warrior

616 Posts

Posted - 2002-07-08 : 10:32:32
This should get you closer to the results:

declare @tblIP table (fldIP varchar(20))

insert @tblIP values('125.0.9.3')
insert @tblIP values('125.0.9.3.0.88.9')
insert @tblIP values('http://123.1.2.3')
insert @tblIP values('62.123.123.123')
insert @tblIP values('123.123.123.123')

select * from @tblIP
where fldIP like '%[0-9]%.[0-9]%.[0-9]%.[0-9]%' -- only numbers allowed
and len(fldIP) - len(replace(fldIP, '.','')) = 3 -- only 3 dots

It will work on varchar and char fields, but not text.
It is not a complete solution, as it will display incorrect IPs and similiar patterns.

HTH
Go to Top of Page

MakeYourDaddyProud

184 Posts

Posted - 2002-07-08 : 10:41:55
Not sure about this...

quote:

select * from @tblIP
where fldIP like '%[0-9]%.[0-9]%.[0-9]%.[0-9]%' -- only numbers allowed
and len(fldIP) - len(replace(fldIP, '.','')) = 3 -- only 3 dots


Would it not match 12A.13B.14C.1DE????

I think it would be very difficult to write an IP parser that will work in every scenario...

HTH


Daniel Small MIAP
www.danielsmall.com IT Factoring
Go to Top of Page
   

- Advertisement -