SQL版IP转换成整数

CREATE FUNCTION f_IP2Int



(



    @ip CHAR(15)



)



RETURNS BIGINT



AS



BEGIN



    DECLARE @re BIGINT   



    SET @re = 0   



    SELECT @re = @re+LEFT(@ip, CHARINDEX('.', @ip+'.')-1)*ID, @ip = STUFF(@ip, 1, CHARINDEX('.', @ip+'.'), '') 



    FROM (



             SELECT ID = CAST(16777216 AS BIGINT) 



             UNION ALL   SELECT 65536 



             UNION ALL   SELECT 256 



             UNION ALL   SELECT 1



         )A 



    RETURN(@re)



END



GO 

/*--调用示例 

   

  select dbo.f_IP2Int('255.255.255.255')  

  select dbo.f_IP2Int('12.168.0.1')  

--*/  

你可能感兴趣的:(sql)