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