Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2007
    Posts
    3

    Unanswered: Parse IP address

    I am fairly new to SQL and am trying to write a function to parse the ip address into 4 sections. I have been searching through the forums to see if anyone has a posted example of parsing an ip address but could not find one.

    I am wondering what would be the best method of doing this, or if anyone has an example.

    Thank you

  2. #2
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    look at SUBSTRING and CHARINDEX in books online and think about how you could nest them.
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  3. #3
    Join Date
    Jul 2007
    Posts
    3
    I found a better answer, builtin function PARSENAME

    takes the 4 sections between the . and seperates them. only 1 line of code for each section:


    ,PARSENAME(ip, 4) AS 'Sec1'
    ,PARSENAME(ip, 3) AS 'Sec2'
    ,PARSENAME(ip, 2) AS 'Sec3'
    ,PARSENAME(ip, 1) AS 'Sec4'


    Thanks

  4. #4
    Join Date
    Dec 2002
    Posts
    1,245
    Holy non-standard usage Batman!

    Just goes to show what a motivated user will do (and drive the developers crazy in the process).

    Kudos to you for the unorthodox approach. Hope it works.

    Regards,

    hmscott
    Have you hugged your backup today?

  5. #5
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Quote Originally Posted by joblenis
    I found a better answer, builtin function PARSENAME

    takes the 4 sections between the . and seperates them. only 1 line of code for each section:


    ,PARSENAME(ip, 4) AS 'Sec1'
    ,PARSENAME(ip, 3) AS 'Sec2'
    ,PARSENAME(ip, 2) AS 'Sec3'
    ,PARSENAME(ip, 1) AS 'Sec4'
    Lol - that is brilliant.
    There have been a few discussions on how to store IP addresses (char, 4 tinyints, integer etc). This is a new way on me to split the human-friendly form though!
    Testimonial:
    pootle flump
    ur codings are working excelent.

  6. #6
    Join Date
    Jul 2007
    Posts
    3
    yup it works great! Def the easiest way to do it

  7. #7
    Join Date
    May 2004
    Location
    Seattle
    Posts
    1,313
    if you have a string with more parts, try this one:

    http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=76033

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •