日期:2014-05-16 浏览次数:21249 次
--1. 字符串IP地址转换成IP数值函数。
CREATE FUNCTION dbo.f_IPtoInt(@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_IPtoInt('192.168.20.179') ip_int
/*
ip_int
--------------------
3232240819
(所影响的行数为 1 行)
*/
drop function dbo.f_IPtoInt
--2. 字符串IP地址转换成IP数值函数。
CREATE FUNCTION dbo.f_InttoIP(@IP bigint)RETURNS varchar(15)
AS
BEGIN
DECLARE @re varchar(15)
SET @re = ''
SELECT @re = @re + '.' + CAST(@IP/ID as varchar) , @IP = @IP % ID
FROM
(
SELECT ID = CAST(16777216 as bigint)
UNION ALL SELECT 65536