- 转载自:
![](http://www.cnrui.cn/blog/attachments/month_0709/m_01.gif)
![](http://www.cnrui.cn/blog/attachments/month_0709/m_02.gif)
![](http://www.cnrui.cn/blog/attachments/month_0709/m_03.gif)
![](http://www.cnrui.cn/blog/attachments/month_0709/m_04.gif)
![](http://www.cnrui.cn/blog/attachments/month_0709/m_05.gif)
![](http://www.cnrui.cn/blog/attachments/month_0709/m_06.gif)
![](http://www.cnrui.cn/blog/attachments/month_0709/m_07.gif)
![](http://www.cnrui.cn/blog/attachments/month_0709/m_08.gif)
![](http://www.cnrui.cn/blog/attachments/month_0709/m_09.gif)
-- 建立IP转换到十进制方法
USE [BasName]GO/****** 对象: UserDefinedFunction [dbo].[X16ToDe] 脚本日期: 09/19/2007 13:56:15 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO-- =============================================-- Author: <Author,,Name>-- Create date: <Create Date, ,>-- Description: 转换IP为十进制-- =============================================Create FUNCTION [dbo].[X16ToDe](@Old_IP nvarchar(15))RETURNS numericASBEGINDECLARE@CharIndex INT,@CurrPoint INT,@SingleValue NVARCHAR(5),@Cache numericSET @CharIndex = 1SET @CurrPoint = CHARINDEX('.',@Old_IP,@CharIndex)SET @SingleValue = SUBSTRING(@Old_IP,@CharIndex,@CurrPoint - @CharIndex)SET @Cache = cast(@SingleValue as numeric)*16777216SET @CharIndex = @CurrPoint + 1SET @CurrPoint = CHARINDEX('.',@Old_IP,@CharIndex)SET @SingleValue = SUBSTRING(@Old_IP,@CharIndex,@CurrPoint - @CharIndex)SET @Cache = @Cache + cast(@SingleValue as numeric)*65536SET @CharIndex = @CurrPoint + 1SET @CurrPoint = CHARINDEX('.',@Old_IP,@CharIndex)SET @SingleValue = SUBSTRING(@Old_IP,@CharIndex,@CurrPoint - @CharIndex)SET @Cache = @Cache + cast(@SingleValue as numeric)*256SET @CharIndex = @CurrPoint + 1SET @SingleValue = SUBSTRING(@Old_IP,@CharIndex,len(@Old_IP)- @CharIndex + 1)SET @Cache = @Cache + cast(@SingleValue as numeric)RETURN @Cache;END这一步你可以自己按照你的情况来做,我是为了加快数据库索引的速度,所以将IP全部转换为十进制,存到一个新表里面。
-- 建立十进制新表
USE [BasName]GO/****** 对象: Table [dbo].[IP_Real] 脚本日期: 09/19/2007 14:01:31 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCreate TABLE [dbo].[IP_Real]([startip] [numeric](18, 0) NULL,[endip] [numeric](18, 0) NULL,[country] [nvarchar](50) NULL,[local] [nvarchar](200) NULL) ON [PRIMARY]-- 格式化省份
Update [BasName].[dbo].[IP]SET [country] = replace([country],N'省',N'省 ')-- 删除CZ88.NETUpdate [BasName].[dbo].[IP]SET [country] = replace([country],N'CZ88.NET',N'')-- 将地区提出Update [BasName].[dbo].[IP]SET [local] = SUBSTRING([country],CHARINDEX(' ',[country],1)+1,len([country]))-- 存为国家或省份Update [BasName].[dbo].[IP]SET [country] = SUBSTRING([country],0,CHARINDEX(' ',[country],1))-- 去处前后导空格Update [BasName].[dbo].[IP]SET [country] = Rtrim(Ltrim([country])) ,[local] = Rtrim(Ltrim([local]))-- 转换IP为十进制,并写入新表Insert INTO [BasName].[dbo].[IP_Real] ([startip] ,[endip] ,[country] ,[local])Select dbo.X16ToDe([startip]) ,dbo.X16ToDe([endip]) ,[country] ,[local]FROM [BasName].[dbo].[IP]order by [startip] ASC最后测试一下看看:
-- 测试
DECLARE @IPNumber numericset @IPNumber = dbo.X16ToDe('219.140.31.91')Select [startip] ,[endip] ,[country] ,[local]FROM [BasName].[dbo].[IP_Real]Where [startip] <= @IPNumber and [endip] >= @IPNumber 注:X16ToDe 转换方法是可以使用的, 但是切割方式不推荐使用 ,纯真版本的很多数据没有规律性