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