The NET_SAFE_IP_FROM_STRING function converts a string representation of an IPv4 or IPv6 address to a binary format.
This function is similar to the NET_IP_FROM_STRING function, but it returns NULL instead of throwing an error if the input parameter is invalid.
Syntax
BINARY NET_SAFE_IP_FROM_STRING(STRING <str>)Parameters
str: Required. The string that represents the IPv4 or IPv6 address to convert. The supported formats are as follows:
IPv4: Dotted-quad format. For example,
10.1.x.x.IPv6: Colon-separated format. For example,
1234:5678:90ab:cdef:1234:5678:90ab:cdef. For more examples, see IP Version 6 Addressing Architecture.
This function does not support CIDR notation, such as 10.1.x.x/xx.
Return value
Returns an IP address of the BINARY type.
If the input parameter is NULL, the function returns NULL.
If the input parameter is invalid, the function returns NULL.
Examples
-- Returns 0123
SELECT NET_SAFE_IP_FROM_STRING('48.49.50.51');
-- Returns =00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=01
SELECT NET_SAFE_IP_FROM_STRING('::1');
-- Returns 0123456789@ABCDE
SELECT NET_SAFE_IP_FROM_STRING('3031:3233:3435:3637:3839:4041:4243:4445');
-- 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');
-- Returns NULL
SELECT NET_SAFE_IP_FROM_STRING(NULL);
-- Returns NULL
SELECT NET_SAFE_IP_FROM_STRING('123.456');
-- Returns NULL
SELECT NET_SAFE_IP_FROM_STRING('::wxyz');Related functions
The NET_SAFE_IP_FROM_STRING function is a network function. For more information about network functions, see Network functions.