1. 程式人生 > >把IP字串轉化為數值格式的SQL Server自定義函式

把IP字串轉化為數值格式的SQL Server自定義函式

create function ip2number (@ip varchar(16))returns bigint
as
begin
set @[email protected]+'.'
declare @pos tinyint
declare @num bigint
declare @bin int,@off int
set @bin=16777216
set @off=0
set @num=0
while @bin>=1
 begin
  set @[email protected]+1
  set @off=charindex('.',@ip,@pos)
  set @[email protected]
+convert(int,substring(@ip,@pos,@[email protected]))*@bin
  set @[email protected]/256
 end
return @num
end
go