SQL binary conversion to point-to-point IP Address

Source: Internet
Author: User
Tags binary to decimal

Set ansi_nulls on
Set quoted_identifier on
Go

/**********************************
* Function: Convert binary to decimal.
* Date: 2009-07-27
* Author:
* Test: Select getipdecimal ('20140901 ')
**********************************/

Alter function [DBO]. [getipdecimal] (@ IP varchar (320 ))
Returns nvarchar (32)
As
Begin
Declare @ ipresult varchar (500) -- returned result
Declare @ result nvarchar (8) -- truncated characters
Declare @ start int -- start position
Declare @ count int -- record the number of cycles
Declare @ jishu int -- Base
Declare @ xinshuju bigint -- new data
Declare @ result1 int
Set @ COUNT = 1
Set @ start = 1
Set @ jishu = 1
Set @ xinshuju = 0
Set @ ipresult =''
While (@ count <= 4)
Begin
Set @ result = substring (@ IP, @ start, 8)
-- Convert to decimal
Set @ result1 = convert (INT, @ result)
While (@ result1> 0)
Begin
Set @ xinshuju = @ xinshuju + (@ result1 % 10) * @ jishu
Set @ result1 = @ result1/10
Set @ jishu = @ jishu * 2
End
Set @ start = @ count * 8 + 1;
Set @ COUNT = @ count + 1;
Set @ ipresult = @ ipresult + convert (varchar (8), @ xinshuju );
If (@ count <> 5)
Set @ ipresult = @ ipresult + '.';
Set @ xinshuju = 0;
Set @ jishu = 1;
End
Return @ ipresult
End




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.