Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Trim Data (IP Address)? 1

Status
Not open for further replies.

kudithipudi

IS-IT--Management
Feb 11, 2002
29
0
0
A2
Hello Guys,
Have a question about trimming data. I have a column in a table with IP addresses as such

IP_Address
192.168.2.51
192.168.4.53
192.168.2.50
192.168.2.49

I want to take this data, remove the fourth octect and inset the new string into another table. Is there a way to do this in SQL? I know the part of inserting the data into another table. But am not sure how to trim the data. Any help is appreciated. Thanks much.

- V.
 
How about
INSERT INTO my_other_table
SELECT SUBSTR(ip_address,1,INSTR(ip_address,'.',3)-1)
FROM my_table;

This would work in Oracle, but I'm not sure about other RDBMSs.
 
Well, not quite!

Try this instead:

INSERT INTO my_other_table
SELECT SUBSTR(ip_address,1,INSTR(ip_address,'.',1,3)-1)
FROM my_table;
 
Carp - Thanks for the answert. But I am using SQL 2000 :(, and the query you have provided does not seem to run in it.

- V.
 
OK - sorry to hear that. But I have to believe that SQL2000 has a substring and instring function, so you should be able to find an equivilent statement without too much trouble.
 
I recommend forum183 for SQL Server questions.

The following T-SQL code will remove the 4th octet from the ip_address.

Select Left(ip_address,len(ip_address)-charindex('.',reverse(ip_address))) If you want to get the best answer for your question read faq183-874 and thread183-468158.
Terry L. Broadbent - DBA
SQL Server Page:
 
tlbroadbent - Thanks for help. Exactly what I was looking for.

- V.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top