NET_IP_NET_MASK
The NET_IP_NET_MASK function generates an IPv4 or IPv6 network mask address based on a specified prefix length.
Syntax
BINARY NET_IP_NET_MASK(BIGINT <num_output_bytes>, BIGINT <prefix_length>)Parameters
num_output_bytes: Required. A BIGINT value that specifies the number of bytes in the network mask. Valid values are 4 for IPv4 and 16 for IPv6.
prefix_length: Required. A BIGINT value that specifies the prefix length of the network mask. The prefix length is the number of bits that are set to 1 in the mask. The value must be in the range of
[0, num_output_bytes × 8].
Return value
Returns an IP address of the BINARY type. You can use this function with the NET_IP_TO_STRING function to convert the address to a readable format. The following rules apply:
If num_output_bytes is not 4 for IPv4 or 16 for IPv6, the function reports an error.
NoteIf the
odps.sql.udf.strict.modeparameter is set to false, the function returns NULL.If prefix_length is a negative value or is greater than
8 × num_output_bytes, the function reports an error.NoteIf the
odps.sql.udf.strict.modeparameter is set to false, the function returns NULL.If num_output_bytes or prefix_length is NULL, the function returns NULL.
Examples
Example 1: Basic example
-- Returns =FF=FF=FF=00
SELECT NET_IP_NET_MASK(4, 24);
-- Returns =FF=FF=F0=00
SELECT NET_IP_NET_MASK(4, 20);
-- Returns =FF=FF=FF=FF
SELECT NET_IP_NET_MASK(4, 32);
-- Returns =00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00
SELECT NET_IP_NET_MASK(16, 0);
-- Returns =80=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00
SELECT NET_IP_NET_MASK(16, 1);
-- Returns =FF=FF=FF=FF=FF=FF=FF=FF=FF=FF=FF=FF=FF=FF=FF=FF
SELECT NET_IP_NET_MASK(16, 128);
-- Returns NULL
SELECT NET_IP_NET_MASK(NULL, 128);
-- Returns NULL
SELECT NET_IP_NET_MASK(4, NULL);Example 2: Use with the NET_IP_TO_STRING function
You can use this function with the NET_IP_TO_STRING function to convert an IP address of the BINARY type to a readable format.
-- Returns 255.255.255.0
SELECT NET_IP_TO_STRING(NET_IP_NET_MASK(4, 24));
-- Returns ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff
SELECT NET_IP_TO_STRING(NET_IP_NET_MASK(16, 128));
-- Returns ffff:ffff:ffff:ffff::
SELECT NET_IP_TO_STRING(NET_IP_NET_MASK(16, 64));Related functions
NET_IP_NET_MASK is a network function. For more information about network functions, see Network functions.