IP转为bigint
create function [dbo].[iptobigint](@ipinfo varchar(16))
returns bigint
as
begin
declare @ret bigint
if PATINDEX(‘%[^.0-9]%‘,@ipinfo)>0
return null
if @ipinfo not like ‘%[0-9].%[0-9].%[0-9].%[0-9]‘
return null
set @ret=0
select @ret=@ret+left(@ipinfo,CHARINDEX(‘.‘,@ipinfo+‘.‘)-1)*ID,
@ipinfo=stuff(@ipinfo,1,CHARINDEX(‘.‘,@ipinfo+‘.‘),‘‘)
from (
SELECT ID=CAST(256*256*256 AS BIGINT)
UNION ALL SELECT 256*256
UNION ALL SELECT 256
UNION ALL SELECT 1)a
return @ret
end
bigint转为ip
CREATE function [dbo].[inttoip](@ipint bigint)
returns varchar(15)
as
begin
declare @ip varchar(15)
set @ip=‘‘
select @ip=@ip+‘.‘+cast(@ipint/ID as varchar)
,@ipint=@ipint%ID
from (
SELECT ID=CAST(256*256*256 AS BIGINT)
UNION ALL SELECT 256*256
UNION ALL SELECT 256
UNION ALL SELECT 1)a
return stuff(@ip,1,1,‘‘)
end
查看定义
print object_definition(object_id(‘dbo.iptobigint‘))