Select record between two IP ranges

前端 未结 8 2173
遇见更好的自我
遇见更好的自我 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:24

    Use below to fetch the ipLow / IPHigh in 4 columns. You can use those columns to compare Ips.

    DECLARE@ip VARCHAR(50)='192.168.0.81' 
    SELECT (SUBSTRING((@ip), 0,
    patindex('%.%',
    (@ip))))
    
    ,
    substring((REPLACE(@ip, (SUBSTRING((@ip), 0,
    patindex('%.%',
    (@ip)) + 1)),
    '')),
    0,
    patindex('%.%',
    ((REPLACE(@ip, (SUBSTRING((@ip), 0,
    patindex('%.%',
    (@ip)) + 1)),
    ''))))),
    SUBSTRING((SUBSTRING(@ip, LEN((SUBSTRING((@ip), 0,
    patindex('%.%',
    (@ip))))) + 2 + LEN(substring((REPLACE(@ip, (SUBSTRING((@ip), 0,
    patindex('%.%',
    (@ip)) + 1)),
    '')),
    0,
    patindex('%.%',
    ((REPLACE(@ip, (SUBSTRING((@ip), 0,
    patindex('%.%',
    (@ip)) + 1)),
    '')))))) + 1,
    LEN(@IP) - 1 - LEN(reverse(SUBSTRING(reverse(@ip), 0,
    patindex('%.%',
    reverse(@ip))))))), 0,
    PATINDEX('%.%',
    (SUBSTRING(@ip, LEN((SUBSTRING((@ip), 0,
    patindex('%.%',
    (@ip))))) + 2 + LEN(substring((REPLACE(@ip, (SUBSTRING((@ip), 0,
    patindex('%.%',
    (@ip)) + 1)),
    '')),
    0,
    patindex('%.%',
    ((REPLACE(@ip, (SUBSTRING((@ip), 0,
    patindex('%.%',
    (@ip)) + 1)),
    '')))))) + 1,
    LEN(@IP) - 1 - LEN(reverse(SUBSTRING(reverse(@ip), 0,
    patindex('%.%',
    reverse(@ip))))))
    
    ))),
    reverse(SUBSTRING(reverse(@ip), 0,
    patindex('%.%',
    reverse(@ip))))
    

提交回复
热议问题