ip_version function

Applies to: check marked yes Databricks Runtime 18.2 and above

Returns the IP version (4 or 6) from an IPv4 or IPv6 address or CIDR block.

Syntax

ip_version ( ip_or_cidr )

Arguments

  • ip_or_cidr: A STRING or BINARY value representing an IPv4 or IPv6 address or CIDR block.

Returns

An INTEGER, either 4 or 6.

The function returns NULL if the input is NULL.

Error conditions

Examples

> SELECT ip_version('192.168.1.5');
  4

> SELECT ip_version('192.168.1.0/24');
  4

> SELECT ip_version('2001:db8::1');
  6

> SELECT ip_version('2001:db8::/32');
  6

> SELECT ip_version('::ffff:192.168.1.1');
  6

> SELECT ip_version(X'C0A80101');
  4

> SELECT ip_version(NULL);
  NULL

> SELECT ip_version('invalid.ip');
  Error: INVALID_IP_ADDRESS_OR_CIDR_BLOCK