论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > Sql Server教程
Tag:注入,存储过程,分页,安全,优化,加密,索引,日志,压缩,base64,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,触发器,socket,安装,sqlserver2000,sqlserver2005,sqlserver2008,视频教程

如何将作为字符串保存的IP地址变为二进制数值?

文章类别:Sql Server | 发表日期:2008-10-5 21:34:40

对于SQL Server 2000,你可以使用下列函数:

CREATE FUNCTION dbo.IPAddrStr2Bin(@strIP varchar(15), @Validate bit = 1)
RETURNS binary(4)
AS
BEGIN
IF @Validate = 1
BEGIN
-- only digits and dots
IF @strIP LIKE '%[^.0-9]%' RETURN (NULL)
-- number of dots must be 3
IF LEN(@strIP) - LEN(REPLACE(@strIP, '.', '')) != 3 RETURN (NULL)
-- all octets must be specified
IF @strIP NOT LIKE '%_%.%_%.%_%.%_%' RETURN (NULL)
END
DECLARE @oct1 binary(1),
@oct2 binary(1),
@oct3 binary(1),
@oct4 binary(1)
SET @oct1 = CAST(CAST(LEFT(@strIP, CHARINDEX('.', @strIP) - 1) AS int) AS
binary(1))
SET @oct2 = CAST(CAST(SUBSTRING(@strIP, CHARINDEX('.', @strIP) + 1,
CHARINDEX('.', @strIP, CHARINDEX('.', @strIP) + 1) - CHARINDEX('.', @strIP)
- 1) AS int) AS binary(1))
SET @oct3 = CAST(CAST(SUBSTRING(@strIP, CHARINDEX('.', @strIP,
CHARINDEX('.', @strIP) + 1) + 1, (LEN(@strIP) - CHARINDEX('.',
REVERSE(@strIP)) + 1) - (CHARINDEX('.', @strIP, CHARINDEX('.', @strIP) + 1))
- 1) AS int) AS binary(1))
SET @oct4 = CAST(CAST(RIGHT(@strIP, LEN(@strIP) - (LEN(@strIP) -
CHARINDEX('.', REVERSE(@strIP)) + 1)) AS int) AS binary(1))
IF @Validate = 1
BEGIN
IF NOT(
(@oct1 BETWEEN 0x01 AND 0xFF) AND
(@oct2 BETWEEN 0x00 AND 0xFF) AND
(@oct3 BETWEEN 0x00 AND 0xFF) AND
(@oct4 BETWEEN 0x00 AND 0xFF)
) RETURN(NULL)
END
RETURN (@oct1 + @oct2 + @oct3 + @oct4)
END
GO

使用例子:
SELECT dbo.IPAddrStr2Bin('172.29.23.2', 0)

对于 SQL Server较早的版本,你可以抽出代码,然后直接使用,或者创建如下所示的存储过程:

CREATE PROCEDURE dbo.spIPAddrStr2Bin
@strIP varchar(15),
@binIP binary(4) OUTPUT,
@Validate bit = 1
AS
IF @Validate = 1
BEGIN
-- only digits and dots
IF @strIP LIKE '%[^.0-9]%' RETURN (NULL)
-- number of dots must be 3
IF LEN(@strIP) - LEN(REPLACE(@strIP, '.', '')) != 3 RETURN (NULL)
-- all octets must be specified
IF @strIP NOT LIKE '%_%.%_%.%_%.%_%' RETURN (NULL)
END
DECLARE @oct1 binary(1),
@oct2 binary(1),
@oct3 binary(1),
@oct4 binary(1)
SET @oct1 = CAST(CAST(LEFT(@strIP, CHARINDEX('.', @strIP) - 1) AS int) AS
binary(1))
SET @oct2 = CAST(CAST(SUBSTRING(@strIP, CHARINDEX('.', @strIP) + 1,
CHARINDEX('.', @strIP, CHARINDEX('.', @strIP) + 1) - CHARINDEX('.',
@strIP) - 1) AS int) AS binary(1))
SET @oct3 = CAST(CAST(SUBSTRING(@strIP, CHARINDEX('.', @strIP,
CHARINDEX('.', @strIP) + 1) + 1, (LEN(@strIP) - CHARINDEX('.',
REVERSE(@strIP)) + 1) - (CHARINDEX('.', @strIP, CHARINDEX('.', @strIP) +
1)) - 1) AS int) AS binary(1))
SET @oct4 = CAST(CAST(RIGHT(@strIP, LEN(@strIP) - (LEN(@strIP) -
CHARINDEX('.', REVERSE(@strIP)) + 1)) AS int) AS binary(1))
IF @Validate = 1
BEGIN
IF NOT(
(@oct1 BETWEEN 0x01 AND 0xFF) AND
(@oct2 BETWEEN 0x00 AND 0xFF) AND
(@oct3 BETWEEN 0x00 AND 0xFF) AND
(@oct4 BETWEEN 0x00 AND 0xFF)
) RETURN(NULL)
END
SET @binIP = @oct1 + @oct2 + @oct3 + @oct4
GO

使用例子:

DECLARE @binIP binary(4)
EXEC dbo.spIPAddrStr2Bin '172.29.23.2', @binIP OUTPUT, 0
PRINT @binIP

注意: 如果你不需要或不想验证自己的字符串IP地址,可以完全删掉对它们进行处理的代码。
视频教程列表
文章教程搜索
 
Sql Server推荐教程
Sql Server热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058