Home > Database > Mysql Tutorial > sql 在查询语句中将整数转为IP字符串实现语句

sql 在查询语句中将整数转为IP字符串实现语句

WBOY
Release: 2016-06-07 17:49:05
Original
1266 people have browsed it

本文章介绍了关于sql 在查询语句中将整数转为IP字符串实现语句,有需要的同学可以参考一下下本文章

数据表的结构是——

 代码如下 复制代码

CREATE TABLE [dbo].[ac_mainctls_new] (

 [id] [int] NULL ,

 [ctlip] [bigint] NULL ,

 [ctlname] [char] (30) COLLATE Chinese_PRC_CI_AS NULL ,

)
 

其中ctlip是设备的IP地址,以整数方式存储。

虽然可以用程序将整数转为IP字符串,但多了一道工序有点麻烦。于是我想直接在SQL查询语句中将其转为IP字符串。

 

经过思索与调试,我完成了该查询语句——

 代码如下 复制代码
SELECT CAST(ctlip / 0x1000000 AS varchar(3))       + '.' + CAST(ctlip / 0x10000 % 0x100 AS varchar(3))       + '.' + CAST(ctlip / 0x100 % 0x100 AS varchar(3))       + '.' + CAST(ctlip % 0x100 AS varchar(3)) AS itr, *FROM ac_mainctls_new

复制代码

 

查询结果为——

ipstr ctlip

 代码如下 复制代码
192.168.10.32 3232238112
192.168.10.35 3232238115
192.168.10.21 3232238101
192.168.10.19 3232238099

 

验证通过。

 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template