IP 數字 互轉 SQL server
阿新 • • 發佈:2018-12-29
ALTER FUNCTION [dbo].[f_Int2IP](
@IP bigint
)RETURNS varchar(16)
AS
BEGIN
DECLARE @re varchar(16)
SET @re=''
SELECT @[email protected]+'.'+CAST(@IP/ID as varchar)
,@[email protected]%ID
from(
SELECT ID=CAST(16777216 as bigint)
UNION ALL SELECT 65536
UNION ALL SELECT 256
UNION ALL SELECT 1)a
RETURN(STUFF(@re,1,1,''))
END
ALTER function [dbo].[f_IP2Int] (@ip varchar(16))returns bigint
as
begin
set @[email protected]+'.'
declare @pos tinyint
declare @num bigint
declare @bin bigint,@off bigint
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(bigint,substring(@ip,@pos,@[email protected]))*@bin
set @[email protected]/256
end
return @num
end