469,076 Members | 1,309 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,076 developers. It's quick & easy.

SQL 2005 Function to Convert Dotted IPs to Long IP Format?

Does anyone have a sample SQL 2005 function the converts a Dotted IP address to its Long IP equivalent?

Example: 192.168.0.1 converts to 3232235521.
dbo.LongIPFuction([dbo].[Asset].[IPAddress])

Scenario:
I have a computer Asset table that contains the IP address of an asset and another table that contains the min and max ip ranges in the Long IP format for each site in our company.

Before I can join the tables in a view I need to convert the Asset Tableís IP to a Long IP that I can compare to the SiteList table.

I have several other tables that need the conversion thatís why I am thinking a function is the best solution.

I have a VBS script that I use for other outside tools that also checks for invalid IPs.
It would be nice to convert it to a SQL function, minus the input box of course.

I apologize if the format of the question is wrong, this is my first post
Thanks for you help in advance!!

[VBS Script]
IpAddressCheck = False
Do Until IpAddressCheck = True
IPaddress = InputBox("Please Enter a Valid Dotted IP Address (Example: 192.168.0.1)")
If IPaddress = "" Then
WScript.Quit
End If
IpAddressCheck = IsValidIP (IPaddress)
Loop
IP = Split(IPaddress,".")
AnswerData = IP(3) + (IP(2) * 256) + (IP(1) * 256 * 256) + (IP(0) * 256 * 256 * 256)

WScript.Echo AnswerData
WScript.Quit

Function IsValidIP (TestIP)
WScript.Echo TestIP
IsValidIP = False
DotCheck = 0
For c1 = 1 To Len(TestIP)
If InStr(Mid(TestIP,c1,1),".") <> 0 Then DotCheck = DotCheck + 1 End If
Next
If DotCheck = 3 Then
IPT = Split(TestIP,".")
If (Asc(IPT(3)) < 58 And Asc(IPT(3)) > 47) And _
(Asc(IPT(2)) < 58 And Asc(IPT(2)) > 47) And _
(Asc(IPT(1)) < 58 And Asc(IPT(1)) > 47) And _
(Asc(IPT(0)) < 58 And Asc(IPT(0)) > 47) Then
If (IPT(3) < 256 And IPT(3) > -1) And _
(IPT(2) < 256 And IPT(2) > -1) And _
(IPT(1) < 256 And IPT(1) > -1) And _
(IPT(0) < 256 And IPT(0) > -1) Then
IsValidIP = True
End If
End If
End If
End Function
Jun 20 '07 #1
0 3220

Post your reply

Sign in to post your reply or Sign up for a free account.

By using this site, you agree to our Privacy Policy and Terms of Use.