Converts a STRING representation of an IPv4 or IPv6 address to BINARY format. Unlike NET_IP_FROM_STRING, this function returns NULL for invalid input instead of throwing an error.
Syntax
BINARY NET_SAFE_IP_FROM_STRING(STRING <str>)Parameters
str: Required. A STRING value representing an IPv4 or IPv6 address.
Supported formats:
| Protocol | Format | Example |
|---|---|---|
| IPv4 | Dotted-decimal notation | 10.1.2.3 |
| IPv6 | Colon-separated notation | 1234:5678:90ab:cdef:1234:5678:90ab:cdef |
| IPv4-mapped IPv6 | Mixed notation | ::ffff:192.0.2.128 |
For the full list of valid IPv6 address formats, see IP Version 6 Addressing Architecture.
Unsupported formats (return NULL):
CIDR notation — for example,
10.1.2.0/24Malformed IPv4 addresses — for example,
123.456Invalid IPv6 addresses — for example,
::wxyz
Return value
Returns an IP address of the BINARY type.
| Input | Return value |
|---|---|
| Valid IPv4 or IPv6 string | BINARY representation of the IP address |
| NULL | NULL |
| Invalid or unsupported format | NULL |
Examples
The following examples cover valid inputs, invalid inputs, and NULL inputs to illustrate the NULL-safe behavior.
-- Valid IPv4 address
-- Returns 0123
SELECT NET_SAFE_IP_FROM_STRING('48.49.50.51');
-- Valid IPv6 loopback address
-- Returns =00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=01
SELECT NET_SAFE_IP_FROM_STRING('::1');
-- Valid full IPv6 address
-- Returns 0123456789@ABCDE
SELECT NET_SAFE_IP_FROM_STRING('3031:3233:3435:3637:3839:4041:4243:4445');
-- Valid IPv4-mapped IPv6 address
-- Returns =00=00=00=00=00=00=00=00=00=00=FF=FF=C0=00=02=80
SELECT NET_SAFE_IP_FROM_STRING('::ffff:192.0.2.128');
-- NULL input — returns NULL
SELECT NET_SAFE_IP_FROM_STRING(NULL);
-- Invalid IPv4 address — returns NULL
SELECT NET_SAFE_IP_FROM_STRING('123.456');
-- Invalid IPv6 address — returns NULL
SELECT NET_SAFE_IP_FROM_STRING('::wxyz');Related functions
NET_SAFE_IP_FROM_STRING is a network function. For more information about network functions, see Network functions.