IP地址转化为数字,charindex ,SUBSTRING
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的更多相关文章
- [转]字符型IP地址转换成数字IP的SQL函数
使用SQL函数可以实现许多的功能,下面为您介绍的是字符型IP地址转换成数字IP的SQL函数示例,供您参考,希望对您学习SQL函数能够有所帮助. /**//*--调用示例 sele ...
- 【Go】IP地址转换:数字与字符串之间高效转换
转载:https://blog.thinkeridea.com/201903/go/ip2long.html IP 地址库中 IP 地址的保存格式一般有两种,一种是点分十进制形式(192.168.1. ...
- 将IP地址转化为整数
$ip = 'IP地址';echo $intip = sprintf('%u',ip2long($ip)); //转换为无符号整型echo long2ip($intip);//将整型转换为ip
- ip地址转化代码实例
/*@author: lgh@ * * */ #include <stdio.h> #include <string.h> #include <unistd.h> ...
- 数字转IP地址函数
--------------------------------------------------------------------- -- Author : htl258(Tony) -- Da ...
- (C#)IP地址与数字地址相互转换
站长网IP查询地址:http://tool.chinaz.com/ip/ 和ip地址转换为数字的工具地址:http://www.msxindl.com/tools/ip/ip_num.asp 可以看到 ...
- 域名转化到IP地址的实现
在linux中,有一些函数可以实现主机名和地址的转化,最常见的有gethostbyname().gethostbyaddr()等,它们都可以实现IPv4和IPv6的地址和主机名之间的转化.其中geth ...
- IP地址与数字地址相互转换
/// <summary> /// IP地址转换成数字 /// </summary> /// <param name="addr">IP地址&l ...
- C# 获取本机IP地址以及转换字符串
/// <summary> /// IP地址转化 /// </summary> /// <param name="ipaddr">整型的IP地址 ...
随机推荐
- [ Linux ] [ OS ] [ memory ] Linux 如何查看系統硬體的記憶體(RAM)資訊
cat /proc/meminfo https://blog.longwin.com.tw/2013/05/linux-ram-memory-info-2013/
- [转]Git入门与实践(一)
git入门与实践(一) · March 10th, 2010 · Posted in UNIX环境编程 · By ghosTM55 Write comment ...
- python tips: is同一性运算符
is用来判断两个对象是不是同一个对象,通过id()函数取对象地址进行判断.python会缓存一些小的整数,以及只包含字母数字以及下划线的字符串.所以在对于这些值的时候,is判断为True. >& ...
- 汇编-理解call,ret
; 有意思的东西,主函数调用子函数用汇编来理解 assume cs:codeseg codeseg segment start: main: call sub1 ; 调用子函数1, push IP1 ...
- Python编程:从入门到实践 - pygal篇 - Die
掷骰子 # die.py 骰子类 from random import randint class Die(): """表示一个骰子的类""" ...
- 使用c++Beep实现春节十二响蜂鸣程序
直接编译运行即可 #include<bits/stdc++.h> #include<windows.h> using namespace std; char a[31][71] ...
- MySQL 与 MongoDB的操作对比
MySQL与MongoDB都是开源的常用数据库,但是MySQL是传统的关系型数据库,MongoDB则是非关系型数据库,也叫文档型数据库,是一种NoSQL的数据库.它们各有各的优点,关键是看用在什么地方 ...
- Ambari-单步创建cluster
Ambari-单步创建 总体介绍 单步创建集群即对于集群内每个服务的安装.开启.组成服务的每个组件host信息的设置等操作都进行一次ambari-server的restAPI的调用. 这样做提高了对集 ...
- openssl之EVP系列之6---EVP_Encrypt系列函数编程架构及样例
openssl之EVP系列之6---EVP_Encrypt系列函数编程架构及样例 ---依据openssl doc/crypto/EVP_EncryptInit.pod和doc/ssleay. ...
- 初识MVC之建项
MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,用一种业务逻辑.数据.界面显示分离的方法组织代码,将业务逻辑聚集到 ...