SET NOCOUNT ON;
declare @I_PCity table
(
IPStart nvarchar(),
Area nvarchar(),
CityID int,
IPID int )
declare @IPStart nvarchar(),
@diana int, --存放点
@dianb int,
@dianc int, @liea bigint, -- 存放列
@lieb bigint,
@liec bigint,
@lied bigint, @intlon bigint --存放化为的整数
insert into @I_PCity(IPStart, Area, CityID, IPID ) select IPStart, Area, CityID, IPID from I_IPCity (nolock) where IPID>= and IPStart is not null and IP3 is null
--
while( exists(select top * from @I_PCity))
begin set @IPStart= (select top IPStart from @I_PCity );
set @diana= charindex('.',@IPStart) --第一个点的下标
set @dianb= charindex('.',@IPStart, @diana + ) --第二个点的下标
set @dianc= charindex('.',@IPStart, @dianb + ) --第三个点的下标 set @liea = SUBSTRING(@IPStart,,@diana-) --第一列的值
set @lieb =SUBSTRING(@IPStart,@diana+,@dianb-@diana- )--第二列的值
set @liec =SUBSTRING(@IPStart,@dianb+,@dianc-@dianb- )--第三列的值
set @lied =SUBSTRING(@IPStart,@dianc+, LEN(@IPStart))--第四列的值
set @intlon= @lied+(@liec*)+(@lieb*)+(@liea*) --拼为整数
update I_IPCity set IP3 =@intlon where IPID= (select top IPID from @I_PCity)
delete top() from @I_PCity
end

返回前台所查到的结果:

USE [91hurong]
GO
/****** Object: StoredProcedure [dbo].[ProIP] Script Date: 08/13/2015 08:40:16 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[ProIP]
-- Add the parameters for the stored procedure here
@str varchar()
AS
BEGIN declare @IPStart nvarchar(), --存放传入的数据
@diana int, --存放点
@dianb int,
@dianc int, @liea bigint, -- 存放列
@lieb bigint,
@liec bigint,
@lied bigint, @intlon bigint --存放化为的整数
set @IPStart= @str; --传入的IP'1.11.6.6'
-- set @IPStart= '1.11.6.6';
set @diana= charindex('.',@IPStart) --第一个点的下标
set @dianb= charindex('.',@IPStart, @diana + ) --第二个点的下标
set @dianc= charindex('.',@IPStart, @dianb + ) --第三个点的下标 set @liea = SUBSTRING(@IPStart,,@diana-) --第一列的值
set @lieb =SUBSTRING(@IPStart,@diana+,@dianb-@diana- )--第二列的值
set @liec =SUBSTRING(@IPStart,@dianb+,@dianc-@dianb- )--第三列的值
set @lied =SUBSTRING(@IPStart,@dianc+, LEN(@IPStart))--第四列的值
set @intlon= @lied+(@liec*)+(@lieb*)+(@liea*) --拼为整数 declare @inta bigint
set @inta=(select top IP3 from I_IPCity where IP3>@intlon and IPStart is not null and IPID is not null and IP3 is not null order by IP3 asc)
select top * from I_IPCity where IP3<@inta and IPStart is not null and IPID is not null and IP3 is not null order by IP3 desc --返回'61.177.117.6'在表中对应的数据
END
exec ProIP '61.177.117.6'

说明:

 'Arg.ea'  对应位置

:     

 charindex('.','Arg.ea') >   --如果大于零,则表示字符串Area中含有字符串CityName; 此例为true

 charindex('.','Arg.ea', )  -- 从第二个位置后,也就是从 字母'r'后开始找,先判断 ‘g’是否为‘.’ ,为否;继续判断‘.’是否为‘.’,此表达式为true

 SUBSTRING('Arg.ea',,)  --截取 字符串 'Arg.ea'中 第一个位置到第二个位置 ;也就是‘Ar’

IP地址转化为数字,charindex ,SUBSTRING的更多相关文章

  1. [转]字符型IP地址转换成数字IP的SQL函数

    使用SQL函数可以实现许多的功能,下面为您介绍的是字符型IP地址转换成数字IP的SQL函数示例,供您参考,希望对您学习SQL函数能够有所帮助.      /**//*--调用示例       sele ...

  2. 【Go】IP地址转换:数字与字符串之间高效转换

    转载:https://blog.thinkeridea.com/201903/go/ip2long.html IP 地址库中 IP 地址的保存格式一般有两种,一种是点分十进制形式(192.168.1. ...

  3. 将IP地址转化为整数

    $ip = 'IP地址';echo $intip = sprintf('%u',ip2long($ip)); //转换为无符号整型echo long2ip($intip);//将整型转换为ip

  4. ip地址转化代码实例

    /*@author: lgh@ * * */ #include <stdio.h> #include <string.h> #include <unistd.h> ...

  5. 数字转IP地址函数

    --------------------------------------------------------------------- -- Author : htl258(Tony) -- Da ...

  6. (C#)IP地址与数字地址相互转换

    站长网IP查询地址:http://tool.chinaz.com/ip/ 和ip地址转换为数字的工具地址:http://www.msxindl.com/tools/ip/ip_num.asp 可以看到 ...

  7. 域名转化到IP地址的实现

    在linux中,有一些函数可以实现主机名和地址的转化,最常见的有gethostbyname().gethostbyaddr()等,它们都可以实现IPv4和IPv6的地址和主机名之间的转化.其中geth ...

  8. IP地址与数字地址相互转换

    /// <summary> /// IP地址转换成数字 /// </summary> /// <param name="addr">IP地址&l ...

  9. C# 获取本机IP地址以及转换字符串

    /// <summary> /// IP地址转化 /// </summary> /// <param name="ipaddr">整型的IP地址 ...

随机推荐

  1. SQL 字段类型详解

    bit    整型 bit数据类型是整型,其值只能是0.1或空值.这种数据类型用于存储只有两种可能值的数据,如Yes 或No.True 或False .On 或Off.    注意:很省空间的一种数据 ...

  2. CUDA 编程实例:计算点云法线

    程序参考文章:http://blog.csdn.net/gamesdev/article/details/17535755  程序优化2 简介:CUDA ,MPI,Hadoop都是并行运算的工具.CU ...

  3. ios 编译版本 最低版本 运行版本 动态链接库

    if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) 运行环境判断: #if __IPHONE_OS_VERSION_ ...

  4. a rel=noopener

    看vue-element-admin的源码的时候,看到a 标签使用  rel=noopener: 然后就很奇怪这个是干什么用的:然后百度到一篇文章,涨知识了. 个人的理解是:不加 rel=noopen ...

  5. Java中类的定义

    成员变量:对应事物的属性 成员方法:对应事物的行为 类定义的格式 定义类:就是定义类的成员,包括成员变量和成员方法 成员变量:和以前定义变量几乎是一样的.只不过位置发生了改变.在类中,方法外. 成员方 ...

  6. 转载:html特殊字符 编码css3 content:&amp;quot;我是特殊符号&amp;quot;

    项目中用到的一些特殊字符和图标 html代码 <div class="cross"></div> css代码 .cross{ width: 20px; he ...

  7. postman测试实例--断言

    postman测试实例--断言 让我们来看看postman测试的一些例子. 其中大部分是作为内部postman片段. 大多数测试是为单行的JavaScript语句一样简单. 只要你想一个请求,你可以有 ...

  8. solrj 操作 solr 单机版

    一.导入 jar 包 <dependency> <groupId>org.apache.solr</groupId> <artifactId>solr- ...

  9. spring bean的作用域和自动装配

    1 Bean的作用域 l  singleton单列:整个容器中只有一个对象实例,每次去访问都是访问同一个对象  默认是单列 l  prototype原型: 每次获取bean都产生一个新的对象,比如Ac ...

  10. Global UNIX file system cylinder group cache

    A global cylinder group (CG) cache is stored in file server memory and shared by a plurality of file ...