---
title: try_ip_as_binary function
description: Learn the syntax of the try\_ip\_as\_binary function of the SQL language in Databricks Runtime.
canonical_url: https://docs.databricks.com/gcp/en/sql/language-manual/functions/try_ip_as_binary
---

# `try_ip_as_binary` function

**Applies to:** ![check marked yes](https://docs.databricks.com/gcp/en/images/icons/check.png) Databricks Runtime 18.2 and above

Returns the canonical binary representation of an IP address or CIDR block. Returns `NULL` instead of raising an error if the input is invalid.

## Syntax

```
try_ip_as_binary ( ip_or_cidr )
```

## Arguments

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

## Returns

A `BINARY` representing the canonical binary form of the IP address or CIDR block. IPv4 addresses are represented as 4 bytes, IPv6 addresses as 16 bytes, and CIDR blocks include an additional byte for the prefix length.

The function returns `NULL` if the input is `NULL` or invalid.

## Examples

```sql
> SELECT hex(try_ip_as_binary('192.168.1.1'));
  C0A80101

> SELECT hex(try_ip_as_binary('192.168.1.5/24'));
  C0A8010018

> SELECT hex(try_ip_as_binary('2001:db8::1'));
  20010DB8000000000000000000000001

> SELECT hex(try_ip_as_binary('::ffff:192.168.1.1'));
  00000000000000000000FFFFC0A80101

> SELECT hex(try_ip_as_binary(X'C0A80101'));
  C0A80101

> SELECT try_ip_as_binary('invalid');
  NULL

> SELECT try_ip_as_binary(X'');
  NULL

> SELECT try_ip_as_binary(NULL);
  NULL
```

## Related functions

* [`ip_as_binary` function](https://docs.databricks.com/gcp/en/sql/language-manual/functions/ip_as_binary.md)
* [`try_ip_as_string` function](https://docs.databricks.com/gcp/en/sql/language-manual/functions/try_ip_as_string.md)
