如何判断一个IP地址在一个IP地址段?
例如
4.43.69.96 ~ 4.208.48.255
有个ip 4.43.69.100
请问如何判断他在这个IP段中。谢谢!
[解决办法]
好像以前讨论过这个问题....
挖挖坟先~
[解决办法]
create function f_checkIP(@IP varchar(20),@IP_ST varchar(20),@IP_ED varchar(20))
returns bit
as
begin
select @IP = right('00'+ParseName(@IP,4),3)+'.'+
right('00'+ParseName(@IP,3),3)+'.'+
right('00'+ParseName(@IP,2),3)+'.'+
right('00'+ParseName(@IP,1),3),
@IP_ST = right('00'+ParseName(@IP_ST,4),3)+'.'+
right('00'+ParseName(@IP_ST,3),3)+'.'+
right('00'+ParseName(@IP_ST,2),3)+'.'+
right('00'+ParseName(@IP_ST,1),3),
@IP_ED = right('00'+ParseName(@IP_ED,4),3)+'.'+
right('00'+ParseName(@IP_ED,3),3)+'.'+
right('00'+ParseName(@IP_ED,2),3)+'.'+
right('00'+ParseName(@IP_ED,1),3)
if (@IP between @IP_ST and @IP_ED)
return 1
return 0
end
go
select dbo.f_checkIP('4.208.48.215','4.43.69.96','4.208.48.255')
select dbo.f_checkIP('4.43.69.95' ,'4.43.69.96','4.208.48.255')
drop function f_checkIP
go
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_IP2Int]') and xtype in (N'FN', N'IF', N'TF'))drop function [dbo].[f_IP2Int]GO--1. 字符串IP地址转换成IP数值函数。CREATE FUNCTION dbo.f_IP2Int(@ip char(15))RETURNS bigintASBEGIN DECLARE @re bigint SET @re=0 SELECT @re=@re+LEFT(@ip,CHARINDEX('.',@ip+'.')-1)*ID ,@ip=STUFF(@ip,1,CHARINDEX('.',@ip+'.'),'') FROM( SELECT ID=CAST(16777216 as bigint) UNION ALL SELECT 65536 UNION ALL SELECT 256 UNION ALL SELECT 1)a RETURN(@re)ENDgoif dbo.f_IP2Int('4.43.69.100' ) between dbo.f_IP2Int('4.43.69.96') and dbo.f_IP2Int('4.208.48.255') print 'OK'else Print 'NO'/*OK*/
[解决办法]
create function f_checkIP(@IP varchar(20),@IP_ST varchar(20),@IP_ED varchar(20))returns bitasbegin select @IP = right('00'+ParseName(@IP,4),3)+'.'+ right('00'+ParseName(@IP,3),3)+'.'+ right('00'+ParseName(@IP,2),3)+'.'+ right('00'+ParseName(@IP,1),3), @IP_ST = right('00'+ParseName(@IP_ST,4),3)+'.'+ right('00'+ParseName(@IP_ST,3),3)+'.'+ right('00'+ParseName(@IP_ST,2),3)+'.'+ right('00'+ParseName(@IP_ST,1),3), @IP_ED = right('00'+ParseName(@IP_ED,4),3)+'.'+ right('00'+ParseName(@IP_ED,3),3)+'.'+ right('00'+ParseName(@IP_ED,2),3)+'.'+ right('00'+ParseName(@IP_ED,1),3) if (@IP between @IP_ST and @IP_ED) return 1 return 0endgoselect dbo.f_checkIP('4.208.48.215','4.43.69.96','4.208.48.255')select dbo.f_checkIP('4.43.69.95' ,'4.43.69.96','4.208.48.255')