MSSQL ip转化查询

--测试数据
create table #IPs(
strIP varchar(15) Null,
binIP binary(4) null
)

insert into #IPs values('0.0.0.0',NULL)
insert into #IPs values('255.255.255.255',NULL)
insert into #IPs values('127.0.0.1',NULL)
insert into #IPs values('192.168.43.192',NULL)
insert into #IPs values('192.168.1.101',NULL)
insert into #IPs values('65.54.239.80',NULL)
insert into #IPs values(NULL,0xB92AEAD3)
insert into #IPs values(NULL,0x2D4B2E53)
insert into #IPs values(NULL,0x31031B0B)
insert into #IPs values(NULL,0x7C2D5F2F)
insert into #IPs values(NULL,0x473E5D31)
insert into #IPs values(NULL,0x90D7D66B)
SELECT strIP,binIP,
cast(cast(substring(binIP,1,1)as int) as varchar(3)) + '.' +
cast(cast(substring(binIP,2,1)as int) as varchar(3)) + '.' +
cast(cast(substring(binIP,3,1)as int) as varchar(3)) + '.' +
cast(cast(substring(binIP,4,1)as int) as varchar(3)),
cast(cast(parsename(strIP,4)as int)as binary(1))+
cast(cast(parsename(strIP,3)as int)as binary(1))+
cast(cast(parsename(strIP,2)as int)as binary(1))+
cast(cast(parsename(strIP,1)as int)as binary(1)),
cast(PARSENAME(strIP,1) as bigint)+
cast(parsename(strIP,2)as bigint)*256 +
cast(parsename(strIP,3)as bigint)*65536 +
cast(parsename(strIP,4)as bigint)*16777216
---int类型也可以,但浪费空间且不直观
from #IPs
drop table #IPs
go

转载于:https://www.cnblogs.com/wenbo/archive/2012/11/10/2763745.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值