I have over 10 million records that contain an IP address in SQL Server. I need to parse out the addresses into a separate column to further analyze them. I have tried using a PATINDEX which works but only covers one pattern for the IP address. The first octet has to be 50, 41, or 107. The other three octets can range from single to triple digits. Here is what I used which catpures 50/41/107.xxx.xxx.xxx:
SELECT SUBSTRING(Column_1,PATINDEX('%[50|41|107].[0-9][0-9][0-9].[0-9][0-9][0-9].[0-9]%',Column_1)-2,14)
FROM table_1
How can I capture all IP patterns (50.x.xxx.xxx, 50.xx.x.xxx, etc...) in one query?
4条答案
按热度按时间jq6vz3qz1#
You can use
PARSENAME
:kokeuurv2#
Use
Left
andCharindex
String function. Try this.hmmo2u0o3#
This query will make sure that you're only capturing IP addresses with only 4 octets.
All octets will need to be in the specified range (currently set to 0-255 but can be changed in the where clause) you can also change the BETWEEN a AND b to IN (a, b, ...) and it will only capture those specific numbers.
It also returns the octets in their own columns as integers which would be a good way to store them moving forward in case you ever needed to do analysis on a specific range, then you won't need to parse text in the future.
Note: Because of the use of TRY_CAST this will only work on SQL Server 2012 and
qqrboqgw4#