Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

Search

XML Feeds

Google Ads

« Npgsql2 the PostgreSQL Net Data provider written 100% in C# has been releasedFree SQL Server Books »
comments
Rate Post:
submit to reddit Digg!FacebookDotnetkicks

Before we start with code let us take a sample IP address, does 127.0.0.1 look familiar? Yes that is your local IP address.

Here it is in decimal and binary
127 0 0 1
01111111 00000000 00000000 00000001

Now to convert, you would take the first value,
add the second value + 256
add the third value + (256 * 256) = 65536
add the fourth value + (256 * 256 * 256) =16777216

So in our case the select would be

  1. SELECT
  2. 1 +
  3. 0 * 256 +
  4. 0 * 65536 +
  5. 127 * 16777216

which is
2130706433

So to convert from IP Adress to integer is very simple, you use PARSENAME to split it up and do the math. Here is the function.

  1. CREATE FUNCTION dbo.IPAddressToInteger (@IP AS VARCHAR(15))
  2. RETURNS BIGINT
  3. AS
  4. BEGIN
  5.  RETURN (CONVERT(BIGINT, PARSENAME(@IP,1)) +
  6.          CONVERT(BIGINT, PARSENAME(@IP,2)) * 256 +
  7.          CONVERT(BIGINT, PARSENAME(@IP,3)) * 65536 +
  8.          CONVERT(BIGINT, PARSENAME(@IP,4)) * 16777216)
  9.  
  10. END
  11. GO

But how do you get 127.0.0.1 out of 2130706433?
It is the reversed of what we did before (surprise) so instead of multiplying we will be dividing
Here is the funcion

  1. CREATE FUNCTION dbo.IntegerToIPAddress (@IP AS BIGINT)
  2. RETURNS VARCHAR(15)
  3. AS
  4. BEGIN
  5.  DECLARE @Octet1 BIGINT
  6.  DECLARE @Octet2 TINYINT
  7.  DECLARE @Octet3 TINYINT
  8.  DECLARE @Octet4 TINYINT
  9.  DECLARE @RestOfIP BIGINT
  10.  
  11.  SET @Octet1 = @IP / 16777216
  12.  SET @RestOfIP = @IP - (@Octet1 * 16777216)
  13.  SET @Octet2 = @RestOfIP / 65536
  14.  SET @RestOfIP = @RestOfIP - (@Octet2 * 65536)
  15.  SET @Octet3 = @RestOfIP / 256
  16.  SET @Octet4 = @RestOfIP - (@Octet3 * 256)
  17.  
  18.  RETURN(CONVERT(VARCHAR, @Octet1) + '.' +
  19.         CONVERT(VARCHAR, @Octet2) + '.' +
  20.         CONVERT(VARCHAR, @Octet3) + '.' +
  21.         CONVERT(VARCHAR, @Octet4))
  22. END

Now let's try this out, first run this

  1. SELECT dbo.IPAddressToInteger('127.0.0.1')

That returns 2130706433
Now run this

  1. SELECT dbo.IntegerToIPAddress(2130706433)

That returns 127.0.0.1

Thanks to K. Brian Kelley for the inspiration for this post, you can also check http://www.truthsolutions.com/ to see some of his books

And also check out the related Order IP Addresses wiki article which I wrote a while ago

About the Author

User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
Social SitingsTwitterFacebookLinkedInHomePageLTD RSS Feed
3164 views
submit to reddit Digg!FacebookDotnetkicks

Comments and Feedback

5 comments

Comment from: Emtucifor [Member] Email
You might change the backquote to a straight single quote. ‘ -> '
10/05/08 @ 14:23
Comment from: SQLDenis [Member] Email
I did change it and it looks fine in code view, bizarre ??
10/05/08 @ 16:17
Comment from: limno [Visitor]
*****
To handle ip address like 255.255.255.255, a minor change needs to be made:
CREATE FUNCTION dbo.IntegerToIPAddress (@IP AS BIGINT)
RETURNS VARCHAR(15)
AS
BEGIN
DECLARE @Octet1 TINYINT
to
DECLARE @Octet1 BIGINT
......
05/15/09 @ 08:58
Comment from: SQLDenis [Member] Email
*****
Thanks limno, I have updated the blog post
05/15/09 @ 09:18
Comment from: Pierre [Visitor] · http://ictoblog.nl
Thanks for this post! Really helped me out while I was trying to figure out how to convert a table with IP addresses stored as BIGINTs back into VARCHAR.
01/29/10 @ 23:34

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)