sql 检查IP是否在指定的IP范围内。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 检查IP是否在指定的IP范围内。相关的知识,希望对你有一定的参考价值。

/*
.SYNOPSIS
    Checks if the IP is in the specified IP range.
.DESCRIPTION
    Checks if the IP is in the specified IP range and returns 1 or 0 (True/False).
.PARAMETER IP
    Specifies the IP.
.PARAMETER IPRange
    Specifies the IP range.
.EXAMPLE
    SELECT dbo.ufn_IsIPInRange('10.10.10.22', '10.10.10.0-10.10.10.254')
.NOTES
    Created by Ioan Popovici (2019-01-14)
    Replace the <CM_Your_Site_Code> with your CM or custom database name.
    Run the code in SQL Server Management Studio.
.LINK
    https://SCCM.Zone
.LINK
    https://SCCM.Zone/Issues
*/

/*##=============================================*/
/*## QUERY BODY                                  */
/*##=============================================*/
/* #region QueryBody */

USE [<CM_Your_Site_Code/Custom_Function_Database>]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO

IF EXISTS (
    SELECT [OBJECT_ID]
    FROM   [SYS].[OBJECTS]
    WHERE  NAME = 'ufn_IsIPInRange'
)
    DROP FUNCTION [dbo].[ufn_IsIPInRange];
GO

CREATE FUNCTION [dbo].[ufn_IsIPInRange] (
    @IP        NVARCHAR(15)
    , @IPRange NVARCHAR(31)
)
RETURNS BIT
AS
    BEGIN

        /* Variable declaration */
        DECLARE @IPRangeStart           AS NVARCHAR(15);
        DECLARE @IPRangeEnd             AS NVARCHAR(15);
        DECLARE @IPToInteger            AS BIGINT;
        DECLARE @IPRangeStartToInteger  AS BIGINT;
        DECLARE @IPRangeEndToInteger    AS BIGINT;
        DECLARE @Result                 AS BIT;

        /* Set IP range start and range end */
        SET @IPRangeStart = (SELECT SUBSTRING(@IPRange, 1, PATINDEX('%-%', @IPRange) -1));
        SET @IPRangeEnd   = (SELECT SUBSTRING(@IPRange, PATINDEX('%-%', @IPRange) +1, LEN(@IPRange)));

        /* Convert IP to integer */
        SET @IPToInteger = (
            CONVERT(BIGINT, PARSENAME(@IP,1)) +
            CONVERT(BIGINT, PARSENAME(@IP,2)) * 256 +
            CONVERT(BIGINT, PARSENAME(@IP,3)) * 65536 +
            CONVERT(BIGINT, PARSENAME(@IP,4)) * 16777216
        );

        /* Convert IP range start to integer */
        SET @IPRangeStartToInteger = (
            CONVERT(BIGINT, PARSENAME(@IPRangeStart,1)) +
            CONVERT(BIGINT, PARSENAME(@IPRangeStart,2)) * 256 +
            CONVERT(BIGINT, PARSENAME(@IPRangeStart,3)) * 65536 +
            CONVERT(BIGINT, PARSENAME(@IPRangeStart,4)) * 16777216
        );

        /* Convert IP range end to integer */
        SET @IPRangeEndToInteger = (
            CONVERT(BIGINT, PARSENAME(@IPRangeEnd,1)) +
            CONVERT(BIGINT, PARSENAME(@IPRangeEnd,2)) * 256 +
            CONVERT(BIGINT, PARSENAME(@IPRangeEnd,3)) * 65536 +
            CONVERT(BIGINT, PARSENAME(@IPRangeEnd,4)) * 16777216
        );

        /* Calculate result */
        SET @Result = (
            CASE
                WHEN @IPToInteger BETWEEN @IPRangeStartToInteger AND @IPRangeEndToInteger
                THEN 1
                ELSE 0
            END
        )

        /* Return result */
        RETURN  @Result;
    END;

/* #endregion */
/*##=============================================*/
/*## END QUERY BODY                              */
/*##=============================================*/

以上是关于sql 检查IP是否在指定的IP范围内。的主要内容,如果未能解决你的问题,请参考以下文章

判断IP地址是否在指定范围内的方法

如何检查IP地址在PHP的两个IP范围内?

如何检查一个IP地址是不是在PHP中的两个IP范围内?

sql 检查IP是否在指定的子网中。

Java计算一个IP地址是不是在指定范围内

检查 IP 地址是不是在范围/子网内的标准/安全方法