> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-fix-nav-issues.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> Documentation for functions used for working with IPv4 and IPv6 addresses.

# Functions for working with IPv4 and IPv6 addresses

{/*AUTOGENERATED_START*/}

<h2 id="IPv4CIDRToRange">
  IPv4CIDRToRange
</h2>

Introduced in: v20.1.0

Takes an IPv4 address with its Classless Inter-Domain Routing (CIDR) prefix length and returns the subnet's address range as a tuple of two IPv4 values: the first and last addresses in that subnet.
For the IPv6 version see [`IPv6CIDRToRange`](#IPv4CIDRToRange).

**Syntax**

```sql theme={null}
IPv4CIDRToRange(ipv4, cidr)
```

**Arguments**

* `ipv4` — IPv4 address. [`IPv4`](/reference/data-types/ipv4) or [`String`](/reference/data-types/string)
* `cidr` — CIDR value. [`UInt8`](/reference/data-types/int-uint)

**Returned value**

Returns a tuple with two IPv4 addresses representing the subnet range. [`Tuple(IPv4, IPv4)`](/reference/data-types/tuple)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT IPv4CIDRToRange(toIPv4('192.168.5.2'), 16);
```

```response title=Response theme={null}
┌─IPv4CIDRToRange(toIPv4('192.168.5.2'), 16)─┐
│ ('192.168.0.0','192.168.255.255')          │
└────────────────────────────────────────────┘
```

<h2 id="IPv4NumToString">
  IPv4NumToString
</h2>

Introduced in: v1.1.0

Converts a 32-bit integer to its IPv4 address string representation in dotted decimal notation (A.B.C.D format).
Interprets the input using big-endian byte ordering.

**Syntax**

```sql theme={null}
IPv4NumToString(num)
```

**Aliases**: `INET_NTOA`

**Arguments**

* `num` — IPv4 address as UInt32 number. [`UInt32`](/reference/data-types/int-uint)

**Returned value**

Returns a number representing the MAC address, or `0` if the format is invalid. [`String`](/reference/data-types/string)

**Examples**

**Usage example**

```sql title=Query theme={null}
IPv4NumToString(3232235521)
```

```response title=Response theme={null}
192.168.0.1
```

<h2 id="IPv4NumToStringClassC">
  IPv4NumToStringClassC
</h2>

Introduced in: v1.1.0

Converts a 32-bit integer to its IPv4 address string representation in dotted decimal notation (A.B.C.D format),
similar to [`IPv4NumToString`](#IPv4NumToString) but using `xxx` instead of the last octet.

**Syntax**

```sql theme={null}
IPv4NumToStringClassC(num)
```

**Arguments**

* `num` — IPv4 address as UInt32 number. [`UInt32`](/reference/data-types/int-uint)

**Returned value**

Returns the IPv4 address string with xxx replacing the last octet. [`String`](/reference/data-types/string)

**Examples**

**Basic example with aggregation**

```sql title=Query theme={null}
SELECT
    IPv4NumToStringClassC(ClientIP) AS k,
    count() AS c
FROM test.hits
GROUP BY k
ORDER BY c DESC
LIMIT 10
```

```response title=Response theme={null}
┌─k──────────────┬─────c─┐
│ 83.149.9.xxx   │ 26238 │
│ 217.118.81.xxx │ 26074 │
│ 213.87.129.xxx │ 25481 │
│ 83.149.8.xxx   │ 24984 │
│ 217.118.83.xxx │ 22797 │
│ 78.25.120.xxx  │ 22354 │
│ 213.87.131.xxx │ 21285 │
│ 78.25.121.xxx  │ 20887 │
│ 188.162.65.xxx │ 19694 │
│ 83.149.48.xxx  │ 17406 │
└────────────────┴───────┘
```

<h2 id="IPv4StringToNum">
  IPv4StringToNum
</h2>

Introduced in: v1.1.0

Converts an IPv4 address string in dotted decimal notation (A.B.C.D format) to its corresponding 32-bit integer representation. (The reverse of [`IPv4NumToString`](#IPv4NumToString)).
If the IPv4 address has an invalid format, an exception is thrown.

**Syntax**

```sql theme={null}
IPv4StringToNum(string)
```

**Aliases**: `INET_ATON`

**Arguments**

* `string` — IPv4 address string. [`String`](/reference/data-types/string)

**Returned value**

Returns theIPv4 address. [`UInt32`](/reference/data-types/int-uint)

**Examples**

**Usage example**

```sql title=Query theme={null}
IPv4StringToNum('192.168.0.1')
```

```response title=Response theme={null}
3232235521
```

<h2 id="IPv4StringToNumOrDefault">
  IPv4StringToNumOrDefault
</h2>

Introduced in: v22.3.0

Converts an IPv4 address string in dotted decimal notation (A.B.C.D format) to its corresponding 32-bit integer representation but if the IPv4 address has an invalid format, it returns `0`.

**Syntax**

```sql theme={null}
IPv4StringToNumOrDefault(string)
```

**Arguments**

* `string` — IPv4 address string. [`String`](/reference/data-types/string)

**Returned value**

Returns the IPv4 address, or `0` if invalid. [`UInt32`](/reference/data-types/int-uint)

**Examples**

**Example with an invalid address**

```sql title=Query theme={null}
SELECT
    IPv4StringToNumOrDefault('127.0.0.1') AS valid,
    IPv4StringToNumOrDefault('invalid') AS invalid;
```

```response title=Response theme={null}
┌──────valid─┬─invalid─┐
│ 2130706433 │       0 │
└────────────┴─────────┘
```

<h2 id="IPv4StringToNumOrNull">
  IPv4StringToNumOrNull
</h2>

Introduced in: v22.3.0

Converts a 32-bit integer to its IPv4 address string representation in dotted decimal notation (A.B.C.D format) but if the IPv4 address has an invalid format, it returns `NULL`.

**Syntax**

```sql theme={null}
IPv4StringToNumOrNull(string)
```

**Arguments**

* `string` — IPv4 address string. [`String`](/reference/data-types/string)

**Returned value**

Returns the IPv4 address, or `NULL` if invalid. [`Nullable(UInt32)`](/reference/data-types/nullable)

**Examples**

**Example with an invalid address**

```sql title=Query theme={null}
SELECT
IPv4StringToNumOrNull('127.0.0.1') AS valid,
IPv4StringToNumOrNull('invalid') AS invalid;
```

```response title=Response theme={null}
┌──────valid─┬─invalid─┐
│ 2130706433 │    ᴺᵁᴸᴸ │
└────────────┴─────────┘
```

<h2 id="IPv4ToIPv6">
  IPv4ToIPv6
</h2>

Introduced in: v1.1.0

Interprets a (big endian) 32-bit number as an IPv4 address, which is then interpreted as the corresponding IPv6 address in `FixedString(16)` format.

**Syntax**

```sql theme={null}
IPv4ToIPv6(x)
```

**Arguments**

* `x` — IPv4 address. [`UInt32`](/reference/data-types/int-uint)

**Returned value**

Returns an IPv6 address in binary format. [`FixedString(16)`](/reference/data-types/fixedstring)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT IPv6NumToString(IPv4ToIPv6(IPv4StringToNum('192.168.0.1'))) AS addr;
```

```response title=Response theme={null}
┌─addr───────────────┐
│ ::ffff:192.168.0.1 │
└────────────────────┘
```

<h2 id="IPv6CIDRToRange">
  IPv6CIDRToRange
</h2>

Introduced in: v20.1.0

Takes an IPv6 address with its Classless Inter-Domain Routing (CIDR) prefix length and returns the subnet's address range as a tuple of two IPv6 values: the lowest and highest addresses in that subnet.
For the IPv4 version see [`IPv4CIDRToRange`](#IPv4CIDRToRange).

**Syntax**

```sql theme={null}
IPv6CIDRToRange(ipv6, cidr)
```

**Arguments**

* `ipv6` — IPv6 address. [`IPv6`](/reference/data-types/ipv6) or [`String`](/reference/data-types/string)
* `cidr` — CIDR value. [`UInt8`](/reference/data-types/int-uint)

**Returned value**

Returns a tuple with two IPv6 addresses representing the subnet range. [`Tuple(IPv6, IPv6)`](/reference/data-types/tuple)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT IPv6CIDRToRange(toIPv6('2001:0db8:0000:85a3:0000:0000:ac1f:8001'), 32);
```

```response title=Response theme={null}
┌─IPv6CIDRToRange(toIPv6('2001:0db8:0000:85a3:0000:0000:ac1f:8001'), 32)─┐
│ ('2001:db8::','2001:db8:ffff:ffff:ffff:ffff:ffff:ffff')                │
└────────────────────────────────────────────────────────────────────────┘
```

<h2 id="IPv6NumToString">
  IPv6NumToString
</h2>

Introduced in: v1.1.0

Converts an IPv6 address from binary format (FixedString(16)) to its standard text representation.
IPv4-mapped IPv6 addresses are displayed in the format `::ffff:111.222.33.44`.

**Syntax**

```sql theme={null}
IPv6NumToString(x)
```

**Aliases**: `INET6_NTOA`

**Arguments**

* `x` — IPv6 address in binary format. [`FixedString(16)`](/reference/data-types/fixedstring) or [`IPv6`](/reference/data-types/ipv6)

**Returned value**

Returns the IPv6 address string in text format. [`String`](/reference/data-types/string)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT IPv6NumToString(toFixedString(unhex('2A0206B8000000000000000000000011'), 16)) AS addr;
```

```response title=Response theme={null}
┌─addr─────────┐
│ 2a02:6b8::11 │
└──────────────┘
```

**IPv6 with hits analysis**

```sql title=Query theme={null}
SELECT
    IPv6NumToString(ClientIP6 AS k),
    count() AS c
FROM hits_all
WHERE EventDate = today() AND substring(ClientIP6, 1, 12) != unhex('00000000000000000000FFFF')
GROUP BY k
ORDER BY c DESC
LIMIT 10
```

```response title=Response theme={null}
┌─IPv6NumToString(ClientIP6)──────────────┬─────c─┐
│ 2a02:2168:aaa:bbbb::2                   │ 24695 │
│ 2a02:2698:abcd:abcd:abcd:abcd:8888:5555 │ 22408 │
│ 2a02:6b8:0:fff::ff                      │ 16389 │
│ 2a01:4f8:111:6666::2                    │ 16016 │
│ 2a02:2168:888:222::1                    │ 15896 │
│ 2a01:7e00::ffff:ffff:ffff:222           │ 14774 │
│ 2a02:8109:eee:ee:eeee:eeee:eeee:eeee    │ 14443 │
│ 2a02:810b:8888:888:8888:8888:8888:8888  │ 14345 │
│ 2a02:6b8:0:444:4444:4444:4444:4444      │ 14279 │
│ 2a01:7e00::ffff:ffff:ffff:ffff          │ 13880 │
└─────────────────────────────────────────┴───────┘
```

**IPv6 mapped IPv4 addresses**

```sql title=Query theme={null}
SELECT
    IPv6NumToString(ClientIP6 AS k),
    count() AS c
FROM hits_all
WHERE EventDate = today()
GROUP BY k
ORDER BY c DESC
LIMIT 10
```

```response title=Response theme={null}
┌─IPv6NumToString(ClientIP6)─┬──────c─┐
│ ::ffff:94.26.111.111       │ 747440 │
│ ::ffff:37.143.222.4        │ 529483 │
│ ::ffff:5.166.111.99        │ 317707 │
│ ::ffff:46.38.11.77         │ 263086 │
│ ::ffff:79.105.111.111      │ 186611 │
│ ::ffff:93.92.111.88        │ 176773 │
│ ::ffff:84.53.111.33        │ 158709 │
│ ::ffff:217.118.11.22       │ 154004 │
│ ::ffff:217.118.11.33       │ 148449 │
│ ::ffff:217.118.11.44       │ 148243 │
└────────────────────────────┴────────┘
```

<h2 id="IPv6StringToNum">
  IPv6StringToNum
</h2>

Introduced in: v1.1.0

Converts an IPv6 address from its standard text representation to binary format (`FixedString(16)`).
Accepts IPv4-mapped IPv6 addresses in the format `::ffff:111.222.33.44.`.
If the IPv6 address has an invalid format, an exception is thrown.

If the input string contains a valid IPv4 address, returns its IPv6 equivalent.
HEX can be uppercase or lowercase.

**Syntax**

```sql theme={null}
IPv6StringToNum(string)
```

**Aliases**: `INET6_ATON`

**Arguments**

* `string` — IPv6 address string. [`String`](/reference/data-types/string)

**Returned value**

Returns theIPv6 address in binary format. [`FixedString(16)`](/reference/data-types/fixedstring)

**Examples**

**Basic example**

```sql title=Query theme={null}
SELECT addr, cutIPv6(IPv6StringToNum(addr), 0, 0) FROM (SELECT ['notaddress', '127.0.0.1', '1111::ffff'] AS addr) ARRAY JOIN addr;
```

```response title=Response theme={null}
┌─addr───────┬─cutIPv6(IPv6StringToNum(addr), 0, 0)─┐
│ notaddress │ ::                                   │
│ 127.0.0.1  │ ::ffff:127.0.0.1                     │
│ 1111::ffff │ 1111::ffff                           │
└────────────┴──────────────────────────────────────┘
```

<h2 id="IPv6StringToNumOrDefault">
  IPv6StringToNumOrDefault
</h2>

Introduced in: v22.3.0

Converts an IPv6 address from its standard text representation to binary format (`FixedString(16)`).
Accepts IPv4-mapped IPv6 addresses in the format `::ffff:111.222.33.44.`.
If the IPv6 address has an invalid format, it returns the default value `::`.

**Syntax**

```sql theme={null}
IPv6StringToNumOrDefault(string)
```

**Arguments**

* `string` — IPv6 address string. [`String`](/reference/data-types/string)

**Returned value**

IPv6 address in binary format, or zero-filled FixedString(16) if invalid. [`FixedString(16)`](/reference/data-types/fixedstring)

**Examples**

**Basic example with invalid address**

```sql title=Query theme={null}
SELECT
    IPv6NumToString(IPv6StringToNumOrDefault('2001:db8::1')) AS valid,
    IPv6NumToString(IPv6StringToNumOrDefault('invalid')) AS invalid;
```

```response title=Response theme={null}
┌─valid───────┬─invalid─┐
│ 2001:db8::1 │ ::      │
└─────────────┴─────────┘
```

<h2 id="IPv6StringToNumOrNull">
  IPv6StringToNumOrNull
</h2>

Introduced in: v22.3.0

Converts an IPv6 address from its standard text representation to binary format (`FixedString(16)`).
Accepts IPv4-mapped IPv6 addresses in the format `::ffff:111.222.33.44.`.
If the IPv6 address has an invalid format, it returns `NULL`.

**Syntax**

```sql theme={null}
IPv6StringToNumOrNull(string)
```

**Arguments**

* `string` — IPv6 address string. [`String`](/reference/data-types/string)

**Returned value**

Returns IPv6 address in binary format, or `NULL` if invalid. [`Nullable(FixedString(16))`](/reference/data-types/nullable)

**Examples**

**Basic example with invalid address**

```sql title=Query theme={null}
SELECT
    IPv6NumToString(IPv6StringToNumOrNull('2001:db8::1')) AS valid,
    IPv6StringToNumOrNull('invalid') AS invalid;
```

```response title=Response theme={null}
┌─valid───────┬─invalid─┐
│ 2001:db8::1 │    ᴺᵁᴸᴸ │
└─────────────┴─────────┘
```

<h2 id="cutIPv6">
  cutIPv6
</h2>

Introduced in: v1.1.0

Accepts a `FixedString(16)` value containing the IPv6 address in binary format.
Returns a string containing the address of the specified number of bytes removed in text format.

**Syntax**

```sql theme={null}
cutIPv6(x, bytesToCutForIPv6, bytesToCutForIPv4)
```

**Arguments**

* `x` — IPv6 address in binary format. [`FixedString(16)`](/reference/data-types/fixedstring) or [`IPv6`](/reference/data-types/ipv6)
* `bytesToCutForIPv6` — Number of bytes to cut for IPv6. [`UInt8`](/reference/data-types/int-uint)
* `bytesToCutForIPv4` — Number of bytes to cut for IPv4. [`UInt8`](/reference/data-types/int-uint)

**Returned value**

Returns a string containing the IPv6 address in text format with specified bytes removed. [`String`](/reference/data-types/string)

**Examples**

**Usage example**

```sql title=Query theme={null}
WITH
    IPv6StringToNum('2001:0DB8:AC10:FE01:FEED:BABE:CAFE:F00D') AS ipv6,
    IPv4ToIPv6(IPv4StringToNum('192.168.0.1')) AS ipv4
SELECT
    cutIPv6(ipv6, 2, 0),
    cutIPv6(ipv4, 0, 2)
```

```response title=Response theme={null}
┌─cutIPv6(ipv6, 2, 0)─────────────────┬─cutIPv6(ipv4, 0, 2)─┐
│ 2001:db8:ac10:fe01:feed:babe:cafe:0 │ ::ffff:192.168.0.0  │
└─────────────────────────────────────┴─────────────────────┘
```

<h2 id="isIPAddressInRange">
  isIPAddressInRange
</h2>

Introduced in: v21.4.0

Determines if an IP address is contained in a network represented in the [Classless Inter-Domain Routing (CIDR)](https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing) notation.

This function accepts both IPv4 and IPv6 addresses (and networks) represented as strings. It returns `0` if the IP version of the address and the CIDR don't match.

**Syntax**

```sql theme={null}
isIPAddressInRange(address, prefix)
```

**Arguments**

* `address` — An IPv4 or IPv6 address. [`String`](/reference/data-types/string)
* `prefix` — An IPv4 or IPv6 network prefix in CIDR. [`String`](/reference/data-types/string)

**Returned value**

Returns `1` if the IP version of the address and the CIDR match, otherwise `0`. [`UInt8`](/reference/data-types/int-uint)

**Examples**

**IPv4 address in range**

```sql title=Query theme={null}
SELECT isIPAddressInRange('127.0.0.1', '127.0.0.0/8')
```

```response title=Response theme={null}
1
```

**IPv4 address not in range**

```sql title=Query theme={null}
SELECT isIPAddressInRange('127.0.0.1', 'ffff::/16')
```

```response title=Response theme={null}
0
```

**IPv6 address not in range**

```sql title=Query theme={null}
SELECT isIPAddressInRange('::ffff:192.168.0.1', '::ffff:192.168.0.4/128')
```

```response title=Response theme={null}
0
```

<h2 id="isIPv4String">
  isIPv4String
</h2>

Introduced in: v21.1.0

Determines whether the input string is an IPv4 address or not.
For the IPv6 version see [`isIPv6String`](#isIPv6String).

**Syntax**

```sql theme={null}
isIPv4String(string)
```

**Arguments**

* `string` — IP address string to check. [`String`](/reference/data-types/string)

**Returned value**

Returns `1` if `string` is IPv4 address, otherwise `0`. [`UInt8`](/reference/data-types/int-uint)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT addr, isIPv4String(addr)
FROM(
SELECT ['0.0.0.0', '127.0.0.1', '::ffff:127.0.0.1'] AS addr
)
ARRAY JOIN addr;
```

```response title=Response theme={null}
┌─addr─────────────┬─isIPv4String(addr)─┐
│ 0.0.0.0          │                  1 │
│ 127.0.0.1        │                  1 │
│ ::ffff:127.0.0.1 │                  0 │
└──────────────────┴────────────────────┘
```

<h2 id="isIPv6String">
  isIPv6String
</h2>

Introduced in: v21.1.0

Determines whether the input string is an IPv6 address or not.
For the IPv4 version see [`isIPv4String`](#isIPv4String).

**Syntax**

```sql theme={null}
isIPv6String(string)
```

**Arguments**

* `string` — IP address string to check. [`String`](/reference/data-types/string)

**Returned value**

Returns `1` if `string` is IPv6 address, otherwise `0`. [`UInt8`](/reference/data-types/int-uint)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT addr, isIPv6String(addr)
FROM(SELECT ['::', '1111::ffff', '::ffff:127.0.0.1', '127.0.0.1'] AS addr)
ARRAY JOIN addr;
```

```response title=Response theme={null}
┌─addr─────────────┬─isIPv6String(addr)─┐
│ ::               │                  1 │
│ 1111::ffff       │                  1 │
│ ::ffff:127.0.0.1 │                  1 │
│ 127.0.0.1        │                  0 │
└──────────────────┴────────────────────┘
```

<h2 id="toIPv4">
  toIPv4
</h2>

Introduced in: v20.1.0

Converts a string or a UInt32 form of IPv4 address to type IPv4.
It is similar to [`IPv4StringToNum`](/reference/functions/regular-functions/ip-address-functions#IPv4StringToNum) and [`IPv4NumToString`](/reference/functions/regular-functions/ip-address-functions#IPv4NumToString) functions but it supports both string and unsigned integer data types as input arguments.

**Syntax**

```sql theme={null}
toIPv4(x)
```

**Arguments**

* `x` — An IPv4 address [`String`](/reference/data-types/string) or [`UInt8/16/32`](/reference/data-types/int-uint)

**Returned value**

Returns an IPv4 address. [`IPv4`](/reference/data-types/ipv4)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT toIPv4('171.225.130.45');
```

```response title=Response theme={null}
┌─toIPv4('171.225.130.45')─┐
│ 171.225.130.45           │
└──────────────────────────┘
```

**Comparison with IPv4StringToNum and IPv4NumToString functions.**

```sql title=Query theme={null}
WITH
    '171.225.130.45' AS IPv4_string
SELECT
    hex(IPv4StringToNum(IPv4_string)),
    hex(toIPv4(IPv4_string))
```

```response title=Response theme={null}
┌─hex(IPv4StringToNum(IPv4_string))─┬─hex(toIPv4(IPv4_string))─┐
│ ABE1822D                          │ ABE1822D                 │
└───────────────────────────────────┴──────────────────────────┘
```

**Conversion from an integer**

```sql title=Query theme={null}
SELECT toIPv4(2130706433);
```

```response title=Response theme={null}
┌─toIPv4(2130706433)─┐
│ 127.0.0.1          │
└────────────────────┘
```

<h2 id="toIPv4OrDefault">
  toIPv4OrDefault
</h2>

Introduced in: v22.3.0

Converts a string or a UInt32 form of an IPv4 address to [`IPv4`](/reference/data-types/ipv4) type.
If the IPv4 address has an invalid format, it returns `0.0.0.0` (0 IPv4), or the provided IPv4 default.

**Syntax**

```sql theme={null}
toIPv4OrDefault(string[, default])
```

**Arguments**

* `string` — IP address string to convert. [`String`](/reference/data-types/string)
* `default` — Optional. The value to return if string is an invalid IPv4 address. [`IPv4`](/reference/data-types/ipv4)

**Returned value**

Returns a string converted to the current IPv4 address, or the default value if conversion fails. [`IPv4`](/reference/data-types/ipv4)

**Examples**

**Valid and invalid IPv4 strings**

```sql title=Query theme={null}
WITH
    '192.168.1.1' AS valid_IPv4_string,
    '999.999.999.999' AS invalid_IPv4_string,
    'not_an_ip' AS malformed_string
SELECT
    toIPv4OrDefault(valid_IPv4_string) AS valid,
    toIPv4OrDefault(invalid_IPv4_string) AS default_value,
    toIPv4OrDefault(malformed_string, toIPv4('8.8.8.8')) AS provided_default;
```

```response title=Response theme={null}
┌─valid─────────┬─default_value─┬─provided_default─┐
│ 192.168.1.1   │ 0.0.0.0       │ 8.8.8.8          │
└───────────────┴───────────────┴──────────────────┘
```

<h2 id="toIPv4OrNull">
  toIPv4OrNull
</h2>

Introduced in: v22.3.0

Converts an input value to a value of type `IPv4` but returns `NULL` in case of an error.
Like [`toIPv4`](#toIPv4) but returns `NULL` instead of throwing an exception on conversion errors.

Supported arguments:

* String representations of IPv4 addresses in dotted decimal notation.
* Integer representations of IPv4 addresses.

Unsupported arguments (return `NULL`):

* Invalid IP address formats.
* IPv6 addresses.
* Out-of-range values.
* Malformed addresses.

**Syntax**

```sql theme={null}
toIPv4OrNull(x)
```

**Arguments**

* `x` — A string or integer representation of an IPv4 address. [`String`](/reference/data-types/string) or [`Integer`](/reference/data-types/int-uint)

**Returned value**

Returns an IPv4 address if successful, otherwise `NULL`. [`IPv4`](/reference/data-types/ipv4) or [`NULL`](/reference/syntax#null)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT
    toIPv4OrNull('192.168.1.1') AS valid_ip,
    toIPv4OrNull('invalid.ip') AS invalid_ip
```

```response title=Response theme={null}
┌─valid_ip────┬─invalid_ip─┐
│ 192.168.1.1 │       ᴺᵁᴸᴸ │
└─────────────┴────────────┘
```

<h2 id="toIPv4OrZero">
  toIPv4OrZero
</h2>

Introduced in: v23.1.0

Converts an input value to a value of type [IPv4](/reference/data-types/ipv4) but returns zero IPv4 address in case of an error.
Like [`toIPv4`](#toIPv4) but returns zero IPv4 address (`0.0.0.0`) instead of throwing an exception on conversion errors.

Supported arguments:

* String representations of IPv4 addresses in dotted decimal notation.
* Integer representations of IPv4 addresses.

Unsupported arguments (return zero IPv4):

* Invalid IP address formats.
* IPv6 addresses.
* Out-of-range values.

**Syntax**

```sql theme={null}
toIPv4OrZero(x)
```

**Arguments**

* `x` — A string or integer representation of an IPv4 address. [`String`](/reference/data-types/string) or [`Integer`](/reference/data-types/int-uint)

**Returned value**

Returns an IPv4 address if successful, otherwise zero IPv4 address (`0.0.0.0`). [`IPv4`](/reference/data-types/ipv4)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT
    toIPv4OrZero('192.168.1.1') AS valid_ip,
    toIPv4OrZero('invalid.ip') AS invalid_ip
```

```response title=Response theme={null}
┌─valid_ip────┬─invalid_ip─┐
│ 192.168.1.1 │ 0.0.0.0    │
└─────────────┴────────────┘
```

<h2 id="toIPv6">
  toIPv6
</h2>

Introduced in: v20.1.0

onverts a string or a `UInt128` form of IPv6 address to [`IPv6`](/reference/data-types/ipv6) type.
For strings, if the IPv6 address has an invalid format, returns an empty value.
Similar to [`IPv6StringToNum`](/reference/functions/regular-functions/ip-address-functions#IPv6StringToNum) and [`IPv6NumToString`](/reference/functions/regular-functions/ip-address-functions#IPv6NumToString) functions, which convert IPv6 address to and from binary format (i.e. `FixedString(16)`).

If the input string contains a valid IPv4 address, then the IPv6 equivalent of the IPv4 address is returned.

**Syntax**

```sql theme={null}
toIPv6(x)
```

**Arguments**

* `x` — An IP address. [`String`](/reference/data-types/string) or [`UInt128`](/reference/data-types/int-uint)

**Returned value**

Returns an IPv6 address. [`IPv6`](/reference/data-types/ipv6)

**Examples**

**Usage example**

```sql title=Query theme={null}
WITH '2001:438:ffff::407d:1bc1' AS IPv6_string
SELECT
    hex(IPv6StringToNum(IPv6_string)),
    hex(toIPv6(IPv6_string));
```

```response title=Response theme={null}
┌─hex(IPv6StringToNum(IPv6_string))─┬─hex(toIPv6(IPv6_string))─────────┐
│ 20010438FFFF000000000000407D1BC1  │ 20010438FFFF000000000000407D1BC1 │
└───────────────────────────────────┴──────────────────────────────────┘
```

**IPv4-to-IPv6 mapping**

```sql title=Query theme={null}
SELECT toIPv6('127.0.0.1');
```

```response title=Response theme={null}
┌─toIPv6('127.0.0.1')─┐
│ ::ffff:127.0.0.1    │
└─────────────────────┘
```

<h2 id="toIPv6OrDefault">
  toIPv6OrDefault
</h2>

Introduced in: v22.3.0

Converts a string or a UInt128 form of IPv6 address to [`IPv6`](/reference/data-types/ipv6) type.
If the IPv6 address has an invalid format, it returns `::` (0 IPv6) or the provided IPv6 default.

**Syntax**

```sql theme={null}
toIPv6OrDefault(string[, default])
```

**Arguments**

* `string` — IP address string to convert. - `default` — Optional. The value to return if string has an invalid format.

**Returned value**

Returns the IPv6 address, otherwise `::` or the provided optional default if argument `string` has an invalid format. [`IPv6`](/reference/data-types/ipv6)

**Examples**

**Valid and invalid IPv6 strings**

```sql title=Query theme={null}
WITH
    '2001:0db8:85a3:0000:0000:8a2e:0370:7334' AS valid_IPv6_string,
    '2001:0db8:85a3::8a2e:370g:7334' AS invalid_IPv6_string,
    'not_an_ipv6' AS malformed_string
SELECT
    toIPv6OrDefault(valid_IPv6_string) AS valid,
    toIPv6OrDefault(invalid_IPv6_string) AS default_value,
    toIPv6OrDefault(malformed_string, toIPv6('::1')) AS provided_default;
```

```response title=Response theme={null}
┌─valid──────────────────────────────────┬─default_value─┬─provided_default─┐
│ 2001:db8:85a3::8a2e:370:7334           │ ::            │ ::1              │
└────────────────────────────────────────┴───────────────┴──────────────────┘
```

<h2 id="toIPv6OrNull">
  toIPv6OrNull
</h2>

Introduced in: v22.3.0

Converts an input value to a value of type `IPv6` but returns `NULL` in case of an error.
Like [`toIPv6`](#toIPv6) but returns `NULL` instead of throwing an exception on conversion errors.

Supported arguments:

* String representations of IPv6 addresses in standard notation.
* String representations of IPv4 addresses (converted to IPv4-mapped IPv6).
* Binary representations of IPv6 addresses.

Unsupported arguments (return `NULL`):

* Invalid IP address formats.
* Malformed IPv6 addresses.
* Out-of-range values.
* Invalid notation.

**Syntax**

```sql theme={null}
toIPv6OrNull(x)
```

**Arguments**

* `x` — A string representation of an IPv6 or IPv4 address. [`String`](/reference/data-types/string)

**Returned value**

Returns an IPv6 address if successful, otherwise `NULL`. [`IPv6`](/reference/data-types/ipv6) or [`NULL`](/reference/syntax#null)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT
    toIPv6OrNull('2001:0db8:85a3:0000:0000:8a2e:0370:7334') AS valid_ipv6,
    toIPv6OrNull('invalid::ip') AS invalid_ipv6
```

```response title=Response theme={null}
┌─valid_ipv6──────────────────────────┬─invalid_ipv6─┐
│ 2001:db8:85a3::8a2e:370:7334        │         ᴺᵁᴸᴸ │
└─────────────────────────────────────┴──────────────┘
```

<h2 id="toIPv6OrZero">
  toIPv6OrZero
</h2>

Introduced in: v23.1.0

Converts an input value to a value of type [IPv6](/reference/data-types/ipv6) but returns zero IPv6 address in case of an error.
Like [`toIPv6`](#toIPv6) but returns zero IPv6 address (`::`) instead of throwing an exception on conversion errors.

Supported arguments:

* String representations of IPv6 addresses in standard notation.
* String representations of IPv4 addresses (converted to IPv4-mapped IPv6).
* Binary representations of IPv6 addresses.

Unsupported arguments (return zero IPv6):

* Invalid IP address formats.
* Malformed IPv6 addresses.
* Out-of-range values.

**Syntax**

```sql theme={null}
toIPv6OrZero(x)
```

**Arguments**

* `x` — A string representation of an IPv6 or IPv4 address. [`String`](/reference/data-types/string)

**Returned value**

Returns an IPv6 address if successful, otherwise zero IPv6 address (`::`).  [`IPv6`](/reference/data-types/ipv6)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT
    toIPv6OrZero('2001:0db8:85a3:0000:0000:8a2e:0370:7334') AS valid_ipv6,
    toIPv6OrZero('invalid::ip') AS invalid_ipv6
```

```response title=Response theme={null}
┌─valid_ipv6──────────────────────────┬─invalid_ipv6─┐
│ 2001:db8:85a3::8a2e:370:7334        │ ::           │
└─────────────────────────────────────┴──────────────┘
```
