Convert SQL point IP address to integer type

Source: Internet
Author: User

Set ansi_nulls on
Set quoted_identifier on
Go

Alter function [DBO]. [getipshijinzhi] (@ IP varchar (32 ))
Returns nvarchar (32)
As
Begin
Declare @ decimalresult bigint -- returned result
Declare @ ipindex bigint -- intercepted IP Address
Declare @ num bigint -- number of cycles
Declare @ index bigint -- Index
Declare @ zhishu bigint -- Exponent
Declare @ ipindexresult bigint -- converted value
Set @ ipindexresult = 0
Set @ decimalresult = 0
Set @ zhishu = 0
Set @ num = 1

-- Intercept IP
While (@ num <5)
Begin
If (@ num = 4)
Set @ ipindex = @ IP
Else
Begin
Set @ Index = patindex ('%. %', @ IP) -- Obtain the. index.
Set @ Index = @ index-1
Set @ ipindex = substring (@ IP, 1, @ index) -- Obtain the intercepted IP address.
End

Set @ ipindexresult = @ ipindex -- assign a value to the result
Set @ zhishu = 32-8 * @ num
-- Convert to integer
While (@ zhishu> 0)
Begin
Set @ ipindexresult = @ ipindexresult * 2
Set @ zhishu = @ ZhiShu-1
End

Set @ num = @ num + 1
Set @ IP = substring (@ IP, @ index + 2, Len (@ IP ))
Set @ decimalresult = @ decimalresult + @ ipindexresult
End
Return @ decimalresult
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.