---
title: try_to_binary function
description: Learn the syntax of the try\_to\_binary function of the SQL language in Databricks.
canonical_url: https://docs.databricks.com/aws/en/sql/language-manual/functions/try_to_binary
---

# `try_to_binary` function

**Applies to:** ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks SQL preview ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks Runtime 11.3 LTS and above

Returns `expr` cast to BINARY based on `fmt`, or NULL if the input is not valid.

## Syntax

```
try_to_binary(expr [, fmt] )
```

## Arguments

* `expr`: A STRING expression to cast.
* `fmt`: A STRING literal describing how to interpret `expr`.

## Returns

A BINARY.

## Notes

If `fmt` is supplied, it must be one of (case-insensitive):

* **`'HEX'`**

  `expr` must be a hexadecimal string.
  Each character must be a hexadecimal digit and there must be an even number of digits.
  The result is the binary representation of the hexadecimal string.

  If `expr` is not a well-formed hexadecimal value the function returns `NULL`.
  Use [to\_binary](https://docs.databricks.com/aws/en/sql/language-manual/functions/to_binary.md) to return an error instead.

* **`'BASE64'`**

  `expr` must be a [RFC 4648 §4: base64 (standard)](https://en.wikipedia.org/wiki/Base64) encoded string.
  The result is the decoded binary data.

* **`'UTF-8'` or `'UTF8'`**

  `expr` is interpreted as a UTF-8 string.
  The result is the binary representation of the string.

The default `fmt` is `'HEX'`.

## Examples

```sql
> SELECT cast(to_binary('537061726B') AS STRING);
  Spark

> SELECT cast(to_binary('hello', 'hex') AS STRING);
  Error: CONVERSION_INVALID_INPUT

> SELECT cast(try_to_binary('hello', 'hex') AS STRING);
  NULL

> SELECT cast(to_binary('537061726B', 'hex') AS STRING);
  Spark

> SELECT cast(to_binary('U3Bhcms=', 'base64') AS STRING);
  Spark

> SELECT cast(to_binary('U3Bhxcms=', 'base64') AS STRING);
  Error: CONVERSION_INVALID_INPUT

> SELECT cast(try_to_binary('U3Bhxcms=', 'base64') AS STRING);
  NULL

> SELECT hex(to_binary('서울시(Seoul)', 'UTF-8'));
  EC849CEC9AB8EC8B9C2853656F756C29
```

## Related functions

* [`base64` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/base64.md)
* [`cast` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/cast.md)
* [`decode` (character set) function](https://docs.databricks.com/aws/en/sql/language-manual/functions/decode_cs.md)
* [`encode` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/encode.md)
* [`hex` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/hex.md)
* [`to_binary` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/to_binary.md)
* [`unbase64` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/unbase64.md)
* [`unhex` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/unhex.md)
