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. 编译OpenCV遇到Qmake问题

    1.Ubuntu安装OpenCv,出现:qmake: could not exec '/usr/lib/x86_64-linux-gnu/qt4/bin/qmake': No such file or ...

  2. 洛谷P3165 [CQOI2014]排序机械臂 Splay维护区间最小值

    可以将高度定义为小数,这样就完美的解决了优先级的问题. Code: #include<cstdio> #include<algorithm> #include<cstri ...

  3. Day 11 函数之对象和名称空间与作用域

    函数对象 函数是第一类对象,即函数可以被当做数据处理 函数对象的四大功能 1.引用 x=100 y=x def func() pass f=func print(f) #打印结果 #<funct ...

  4. with as递归调用

    一.递归调用--在代码中偶尔看到以记之,便于下次学习 https://blog.csdn.net/johnf_nash/article/details/78681060 --查询节点及其下所有子节点 ...

  5. 序列模型(5)-----双向神经网络(BRNN)和深层循环神经网络(Deep RNN)

    一.双向循环神经网络BRNN 采用BRNN原因: 双向RNN,即可以从过去的时间点获取记忆,又可以从未来的时间点获取信息.为什么要获取未来的信息呢? 判断下面句子中Teddy是否是人名,如果只从前面两 ...

  6. C语言提高 (3) 第三天 二级指针的三种模型 栈上指针数组、栈上二维数组、堆上开辟空间

    1 作业讲解 指针间接操作的三个必要条件 两个变量 其中一个是指针 建立关联:用一个指针指向另一个地址 * 简述sizeof和strlen的区别 strlen求字符串长度,字符数组到’\0’就结束 s ...

  7. Unsupported platform for fsevents@1.2.3: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})

    系统:win10 使用 npm 安装依赖时报错: Unsupported platform for fsevents@1.2.3: wanted {"os":"darwi ...

  8. Project Euler 35 Circular primes

    题意:197被称为圆周素数,因为将它逐位旋转所得到的数:197/971和719都是素数.小于100的圆周素数有十三个:2.3.5.7.11.13.17.31.37.71.73.79和97.小于一百万的 ...

  9. 训练1-J

    把一个偶数拆成两个不同素数的和,有几种拆法呢? Input 输入包含一些正的偶数,其值不会超过10000,个数不会超过500,若遇0,则结束. Output 对应每个偶数,输出其拆成不同素数的个数,每 ...

  10. NOI 2016 循环之美 (莫比乌斯反演+杜教筛)

    题目大意:略 洛谷传送门 鉴于洛谷最近总崩,附上良心LOJ链接 任何形容词也不够赞美这一道神题 $\sum\limits_{i=1}^{N}\sum\limits_{j=1}^{M}[gcd(i,j) ...