IP 数字 互转 SQL server



ALTER FUNCTION [dbo].[f_Int2IP](
@IP bigint
)RETURNS varchar(16)
AS
BEGIN
    DECLARE @re varchar(16)
    SET @re=''
    SELECT @re=@re+'.'+CAST(@IP/ID as varchar)
        ,@IP=@IP%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 @ip=@ip+'.'
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 @pos=@off+1
  set @off=charindex('.',@ip,@pos)
  set @num=@num+convert(bigint,substring(@ip,@pos,@off-@pos))*@bin
  set @bin=@bin/256
 end
return @num
end

评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符 “速评一下”
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页