Select record between two IP ranges

前端 未结 8 2176
遇见更好的自我
遇见更好的自我 2021-02-19 05:14

I have a table which stores a ID, Name, Code, IPLow, IPHigh such as:

1, Lucas, 804645, 192.130.1         


        
相关标签:
8条回答
  • 2021-02-19 05:36

    Try this simple way checking range

    DECLARE @IP NVARCHAR(30)='192.168.500.1'
    
    SELECT  * FROM 
    Branches
    WHERE
    CAST (PARSENAME(@IP,4) AS INT)>=CAST(PARSENAME(IPLow,4) AS INT) AND CAST(PARSENAME(@IP,3) AS INT)>=CAST(PARSENAME(IPLow,3) AS INT) AND CAST(PARSENAME(@IP,2) AS INT)>=CAST(PARSENAME(IPLow,2) AS INT) AND CAST(PARSENAME(@IP,1) AS INT)>=CAST(PARSENAME(IPLow,1) AS INT)
    AND
    CAST(PARSENAME( @IP,4) AS INT) <= CAST(PARSENAME(IPHigh ,4) AS INT) AND CAST(PARSENAME(@IP ,3) AS INT) <=CAST(PARSENAME(IPHigh ,3) AS INT) AND CAST(PARSENAME(@IP ,2) AS INT) <=CAST(PARSENAME(IPHigh ,2) AS INT) AND CAST(PARSENAME(@IP ,1) AS INT)<=CAST(PARSENAME(IPHigh ,1) AS INT)
    

    AS Per @Ed Haper Comment Cast is needed.

    0 讨论(0)
  • 2021-02-19 05:45

    Depends on which record you are looking for the high or the low.

    select * from table where IPlow like '192.168.2.50' or IPHigh like '192.168.2.50'
    
    0 讨论(0)
提交回复
热议问题