sql 中写个将Ip转换为数值的函数?

解决方案 »

  1.   

    我写过,你可以试试这个:CREATE FUNCTION [dbo].[F_IPAddressToInt64]
    (
    @IPAddress varchar(20) -- IP地址
    )RETURNS bigint ASBEGIN 
    declare @No1Dot int
    declare @No2Dot int
    declare @No3Dot int select @No1Dot = charindex('.', @IPAddress, 1)
    if (@No1Dot = 0)
    begin
    return 0
    end select @No2Dot = charindex('.', @IPAddress, @No1Dot + 1)
    if (@No2Dot = 0)
    begin
    return 0
    end select @No3Dot = charindex('.', @IPAddress, @No2Dot + 1)
    if (@No3Dot = 0)
    begin
    return 0
    end declare @Num1 bigint
    declare @Num2 bigint
    declare @Num3 bigint
    declare @Num4 bigint set @Num1 = cast(substring(@IPAddress, 1, @No1Dot - 1) as int)
    set @Num2 = cast(substring(@IPAddress, @No1Dot + 1, @No2Dot - @No1Dot - 1) as int)
    set @Num3 = cast(substring(@IPAddress, @No2Dot + 1, @No3Dot - @No2Dot - 1) as int)
    set @Num4 = cast(substring(@IPAddress, @No3Dot + 1, len(@IPAddress) - @No3Dot) as int) return (@Num1 * 256 *256 * 256) + (@Num2 * 256 * 256) + (@Num3 * 256) + @Num4
    END