Converts an IPv4 or IPv6 address from BINARY to STRING format, returning a human-readable IP address string.
Syntax
STRING NET_IP_TO_STRING(BINARY <addr>)Parameters
| Parameter | Required | Type | Description |
|---|---|---|---|
addr | Yes | BINARY | The IPv4 or IPv6 address to convert. Must be a valid 4-byte (IPv4) or 16-byte (IPv6) binary value. |
Return value
Returns a STRING value representing the IP address. The following rules apply:
If
addris 4 bytes, the function returns the corresponding IPv4 address.If
addris 16 bytes, the function returns the corresponding IPv6 address.If
addris not a valid IPv4 or IPv6 binary value, the function returns an error.If
addrisNULL, the function returnsNULL.
Examples
Example 1: Convert a binary value to an IPv4 address
The following example uses NET_IP_NET_MASK to generate a 4-byte binary value, then converts it to an IPv4 address string.
SELECT NET_IP_TO_STRING(NET_IP_NET_MASK(4, 24));
-- Returns: 255.255.255.0Example 2: Convert a binary value to an IPv6 address
The following example converts a 16-byte hex-encoded binary value to an IPv6 address string.
SELECT NET_IP_TO_STRING(CAST(unhex('00000000000000000000FFFFC0000280') AS BINARY));
-- Returns: ::ffff:192.0.2.128Example 3: Handle a NULL input
SELECT NET_IP_TO_STRING(NULL);
-- Returns: NULLRelated functions
NET_IP_TO_STRING is a network function. For more information about network functions, see Network functions.