title | description | ms.reviewer | ms.topic | ms.date |
---|---|---|---|---|
ipv4_compare() |
Learn how to use the ipv4_compare() function to compare two IPv4 strings. |
alexans |
reference |
12/28/2022 |
Compares two IPv4 strings. The two IPv4 strings are parsed and compared while accounting for the combined IP-prefix mask calculated from argument prefixes, and the optional PrefixMask
argument.
ipv4_compare(
Expr1,
Expr2[ ,
PrefixMask])
Name | Type | Required | Description |
---|---|---|---|
Expr1, Expr2 | string |
✔️ | A string expression representing an IPv4 address. IPv4 strings can be masked using IP-prefix notation. |
PrefixMask | int |
An integer from 0 to 32 representing the number of most-significant bits that are taken into account. |
[!INCLUDE ip-prefix-notation]
0
: If the long representation of the first IPv4 string argument is equal to the second IPv4 string argument1
: If the long representation of the first IPv4 string argument is greater than the second IPv4 string argument-1
: If the long representation of the first IPv4 string argument is less than the second IPv4 string argumentnull
: If conversion for one of the two IPv4 strings wasn't successful.
[!div class="nextstepaction"] Run the query
datatable(ip1_string:string, ip2_string:string)
[
'192.168.1.0', '192.168.1.0', // Equal IPs
'192.168.1.1/24', '192.168.1.255', // 24 bit IP-prefix is used for comparison
'192.168.1.1', '192.168.1.255/24', // 24 bit IP-prefix is used for comparison
'192.168.1.1/30', '192.168.1.255/24', // 24 bit IP-prefix is used for comparison
]
| extend result = ipv4_compare(ip1_string, ip2_string)
Output
ip1_string | ip2_string | result |
---|---|---|
192.168.1.0 | 192.168.1.0 | 0 |
192.168.1.1/24 | 192.168.1.255 | 0 |
192.168.1.1 | 192.168.1.255/24 | 0 |
192.168.1.1/30 | 192.168.1.255/24 | 0 |
Compare IPs using IP-prefix notation specified inside the IPv4 strings and as additional argument of the ipv4_compare()
function
[!div class="nextstepaction"] Run the query
datatable(ip1_string:string, ip2_string:string, prefix:long)
[
'192.168.1.1', '192.168.1.0', 31, // 31 bit IP-prefix is used for comparison
'192.168.1.1/24', '192.168.1.255', 31, // 24 bit IP-prefix is used for comparison
'192.168.1.1', '192.168.1.255', 24, // 24 bit IP-prefix is used for comparison
]
| extend result = ipv4_compare(ip1_string, ip2_string, prefix)
Output
ip1_string | ip2_string | prefix | result |
---|---|---|---|
192.168.1.1 | 192.168.1.0 | 31 | 0 |
192.168.1.1/24 | 192.168.1.255 | 31 | 0 |
192.168.1.1 | 192.168.1.255 | 24 | 0 |
- Overview of IPv4/IPv6 functions
- Overview of IPv4 text match functions