

# Blob and encoding functions

Use these functions to convert text to and from its UTF-8 bytes, to write bytes as base64, hexadecimal, or binary digits, and to read such text back into a `BLOB`. The `hex` and `bin` functions also write an integer in base 16 or base 2. You can also get the size of a blob in bytes, and repeat a blob, a string, or a list.

## `bin` function
<a name="athena-duckdb-functions-binary-bin"></a>

Returns a string of `0` and `1` characters. For an integer, the result is the number in base 2, with no leading zeros. For a `VARCHAR` value, the result has 8 digits for each byte of the string, in order. If `value` is `NULL`, the result is `NULL`.

A negative `BIGINT`, `HUGEINT`, or smaller integer value is shown in two's complement form. The result then has 64 digits for `BIGINT` and smaller integer types, and 128 digits for `HUGEINT`. A negative `BIGNUM` value is shown as a minus sign followed by the binary digits of its absolute value, so `bin(CAST(-5 AS BIGNUM))` returns `-101`.

`to_binary` is an alias of `bin`.

### Syntax
<a name="athena-duckdb-functions-binary-bin-syntax"></a>

```sql
bin({{value}})
```

### Arguments
<a name="athena-duckdb-functions-binary-bin-arguments"></a>

The `bin` function takes the following argument:

**{{value}}**  
The value to convert to binary digits. It can be an integer value, such as `INTEGER`, `BIGINT`, `UBIGINT`, `HUGEINT`, `UHUGEINT`, or `BIGNUM`, or a `VARCHAR` value. It can also be a `DOUBLE` or `REAL` value, which `bin` rounds to the nearest integer first.

### Return type
<a name="athena-duckdb-functions-binary-bin-return-type"></a>

`VARCHAR`

### Examples
<a name="athena-duckdb-functions-binary-bin-examples"></a>

The following SQL query returns the binary digits of the number `42`.

```sql
SELECT bin(42) AS result
```

The query returns the following result.


| result | 
| --- | 
| `101010` | 

The following SQL query uses the `to_binary` alias to convert a string. The letter `A` is the byte `01000001`, and the letter `a` is the byte `01100001`.

```sql
SELECT to_binary('Aa') AS result
```

The query returns the following result.


| result | 
| --- | 
| `0100000101100001` | 

The following SQL query compares the number `42` with the string `'42'`. The string is converted byte by byte, so the results differ.

```sql
SELECT bin(42) AS from_number, bin('42') AS from_string
```

The query returns the following result.


| from\_number | from\_string | 
| --- | --- | 
| `101010` | `0011010000110010` | 

The following SQL query converts the negative number `-5`. The result has 64 digits, so the query shows its length and its last 8 digits.

```sql
SELECT
    length(bin(-5)) AS digits,
    right(bin(-5), 8) AS last_8_digits
```

The query returns the following result.


| digits | last\_8\_digits | 
| --- | --- | 
| `64` | `11111011` | 

### Considerations
<a name="athena-duckdb-functions-binary-bin-considerations"></a>

A number inside a string is converted as text. `bin('42')` returns the bits of the characters `4` and `2`, not the bits of the number 42. To get the bits of the number, cast the string to `BIGINT` first.

`bin` does not accept a `DECIMAL` value, such as the literal `3.7`. Cast the value to an integer type first, for example `bin(CAST(3.7 AS BIGINT))`, which returns `100`. A `DOUBLE` or `REAL` value is rounded to the nearest integer and converted as a `BIGNUM`. A value exactly halfway between two integers is rounded to the even one, so `bin(CAST(2.5 AS DOUBLE))` returns `10`. A negative value gets a minus sign instead of two's complement digits, so `bin(CAST(-5 AS DOUBLE))` returns `-101`.

To pad the result with leading zeros to a fixed width, use the [`lpad` function](duckdb-functions-string-general.md#athena-duckdb-functions-string-lpad), for example `lpad(bin(5), 8, '0')`, which returns `00000101`.

## `decode` function
<a name="athena-duckdb-functions-binary-decode"></a>

Returns the bytes of `blob` read as UTF-8 text, as a `VARCHAR` value. Use it to turn a `BLOB` back into text, for example the result of `encode` or `from_base64`. If `blob` is `NULL`, the result is `NULL`.

By default, the query fails if `blob` holds bytes that are not valid UTF-8. With `error_option`, you can replace each invalid byte with a question mark (`?`) or leave the invalid bytes out instead. A byte can start a multi-byte UTF-8 character that the bytes after it do not complete. In that case, `'replace'` can also turn the next valid character into a question mark. That character is then lost. For example, `decode('ab\xC3cd'::BLOB, 'replace')` returns `ab??d`, without the `c`. `'ignore'` keeps that character, so `decode('ab\xC3cd'::BLOB, 'ignore')` returns `abcd`.

### Syntax
<a name="athena-duckdb-functions-binary-decode-syntax"></a>

```sql
decode({{blob}} [, {{error_option}} ])
```

### Arguments
<a name="athena-duckdb-functions-binary-decode-arguments"></a>

The `decode` function takes the following arguments:

**{{blob}}**  
The bytes to convert, as a `BLOB` value.

**{{error\_option}}**  
The option that sets what happens to bytes that are not valid UTF-8, as a `VARCHAR` value. With `'strict'`, the default, the query fails. With `'replace'`, each invalid byte becomes `?`. If a byte starts a multi-byte character that the bytes after it do not complete, `'replace'` can also turn the next valid character into `?`. With `'ignore'`, the function leaves the invalid bytes out of the result and keeps the valid characters. This argument is optional.

### Return type
<a name="athena-duckdb-functions-binary-decode-return-type"></a>

`VARCHAR`

### Examples
<a name="athena-duckdb-functions-binary-decode-examples"></a>

The following SQL query converts a `BLOB` of two bytes, written in hexadecimal, to text. The two bytes are the UTF-8 codes for the letters `h` and `i`.

```sql
SELECT decode('\x68\x69'::BLOB) AS result
```

The query returns the following result.


| result | 
| --- | 
| `hi` | 

The following SQL query decodes the two UTF-8 bytes of the letter u with a diaeresis, and compares the result with that letter, `chr(252)`.

```sql
SELECT
    decode(
        '\xC3\xBC'::BLOB
    ) = chr(252) AS is_u_diaeresis
```

The query returns the following result.


| is\_u\_diaeresis | 
| --- | 
| `true` | 

The following SQL query decodes a `BLOB` with one byte, `\xA0`, that is not valid UTF-8. With `'replace'`, the byte becomes `?`. With `'ignore'`, it is left out.

```sql
SELECT
    decode('ab\xA0cd'::BLOB, 'replace') AS replaced,
    decode('ab\xA0cd'::BLOB, 'ignore') AS ignored
```

The query returns the following result.


| replaced | ignored | 
| --- | --- | 
| `ab?cd` | `abcd` | 

The following SQL query wraps the call in `TRY`, so the expression returns `NULL` instead of failing on the byte that is not valid UTF-8.

```sql
SELECT TRY(decode('\xA0'::BLOB)) IS NULL AS is_null
```

The query returns the following result.


| is\_null | 
| --- | 
| `true` | 

### Considerations
<a name="athena-duckdb-functions-binary-decode-considerations"></a>

To get `NULL` instead of an error for a value that is not valid UTF-8, wrap the call in `TRY`, as in `TRY(decode({{blob}}))`.

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-binary-decode-differences-from-engine-version-3"></a>

In Athena engine version 3, you convert binary data to text with `from_utf8`, which replaces invalid UTF-8 with the Unicode replacement character (U\+FFFD) and does not fail. DuckDB does not have `from_utf8`. Use `decode`, which fails on invalid UTF-8 unless you pass `'replace'` or `'ignore'`. With `'replace'`, each invalid byte becomes a question mark (`?`), not U\+FFFD. When a multi-byte character is cut short, that option can also turn the next valid character into a question mark.

## `encode` function
<a name="athena-duckdb-functions-binary-encode"></a>

Returns the UTF-8 bytes of `string` as a `BLOB`. A character outside the ASCII range becomes two or more bytes, so the result can have more bytes than `string` has characters. If `string` is `NULL`, the result is `NULL`.

Use `encode` to pass text to a function that takes a `BLOB`, such as `to_base64` or `octet_length`. To convert the bytes back to text, use the [`decode` function](#athena-duckdb-functions-binary-decode).

### Syntax
<a name="athena-duckdb-functions-binary-encode-syntax"></a>

```sql
encode({{string}})
```

### Arguments
<a name="athena-duckdb-functions-binary-encode-arguments"></a>

The `encode` function takes the following argument:

**{{string}}**  
The string to convert, as a `VARCHAR` value.

### Return type
<a name="athena-duckdb-functions-binary-encode-return-type"></a>

`BLOB`

### Examples
<a name="athena-duckdb-functions-binary-encode-examples"></a>

The following SQL query converts a string to a `BLOB`. Each byte is a printable ASCII character, so the result shows the same text.

```sql
SELECT encode('abc') AS result
```

The query returns the following result.


| result | 
| --- | 
| `abc` | 

The following SQL query converts a string that ends with the letter e with an acute accent, `chr(233)`. That letter becomes two bytes, which the result shows in `\x` form.

```sql
SELECT encode('caf' || chr(233)) AS result
```

The query returns the following result.


| result | 
| --- | 
| `caf\xC3\xA9` | 

### Considerations
<a name="athena-duckdb-functions-binary-encode-considerations"></a>

`encode` accepts only `VARCHAR` values. To encode a number, cast it first, as in `encode(CAST(123 AS VARCHAR))`.

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-binary-encode-differences-from-engine-version-3"></a>

In Athena engine version 3, you convert a string to its UTF-8 bytes with `to_utf8`, which returns `VARBINARY`. DuckDB does not have `to_utf8`. Use `encode`, which returns `BLOB`.

## `from_base64` function
<a name="athena-duckdb-functions-binary-from-base64"></a>

Decodes `string`, which holds base64-encoded data, and returns the decoded bytes as a `BLOB`. If `string` is `NULL`, the result is `NULL`.

To turn decoded UTF-8 bytes into text, pass the result to `decode`, which returns a `VARCHAR`. To encode a value as base64, use the [`to_base64` function](#athena-duckdb-functions-binary-to-base64).

### Syntax
<a name="athena-duckdb-functions-binary-from-base64-syntax"></a>

```sql
from_base64({{string}})
```

### Arguments
<a name="athena-duckdb-functions-binary-from-base64-arguments"></a>

The `from_base64` function takes the following argument:

**{{string}}**  
The base64-encoded data to decode, as a `VARCHAR` value.

### Return type
<a name="athena-duckdb-functions-binary-from-base64-return-type"></a>

`BLOB`

### Examples
<a name="athena-duckdb-functions-binary-from-base64-examples"></a>

The following SQL query decodes the base64 string `QQ==` into a `BLOB` that holds one byte, which the result shows as the letter `A`.

```sql
SELECT from_base64('QQ==') AS result
```

The query returns the following result.


| result | 
| --- | 
| `A` | 

The following SQL query decodes a base64 string and converts the bytes to `VARCHAR` with `decode`.

```sql
SELECT decode(from_base64('aGVsbG8=')) AS result
```

The query returns the following result.


| result | 
| --- | 
| `hello` | 

### Considerations
<a name="athena-duckdb-functions-binary-from-base64-considerations"></a>

If `string` is not valid base64, for example if its length is not a multiple of 4 or it holds a character outside the base64 alphabet, the query fails.

## `hex` function
<a name="athena-duckdb-functions-binary-hex"></a>

Returns `value` as a string of uppercase hexadecimal digits. For an integer, the result is the number in base 16, without leading zeros, so `hex(42)` returns `2A`. For a `VARCHAR` value, the result has two digits for each byte of the string's UTF-8 encoding. For a `BLOB` value, the result has two digits for each byte. If `value` is `NULL`, the result is `NULL`.

`to_hex` is an alias of `hex`. To convert hexadecimal digits back to bytes, use the [`unhex` function](#athena-duckdb-functions-binary-unhex). For lowercase digits, pass the result to `lower`.

### Syntax
<a name="athena-duckdb-functions-binary-hex-syntax"></a>

```sql
hex({{value}})
```

### Arguments
<a name="athena-duckdb-functions-binary-hex-arguments"></a>

The `hex` function takes the following argument:

**{{value}}**  
The value to convert to hexadecimal digits. It can be an integer value, such as `INTEGER`, `BIGINT`, `UBIGINT`, `HUGEINT`, `UHUGEINT`, or `BIGNUM`, or a `VARCHAR` or `BLOB` value. It can also be a `DOUBLE` or `FLOAT` value, which `hex` rounds to the nearest integer first.

### Return type
<a name="athena-duckdb-functions-binary-hex-return-type"></a>

`VARCHAR`

### Examples
<a name="athena-duckdb-functions-binary-hex-examples"></a>

The following SQL query converts the integer `42` to hexadecimal.

```sql
SELECT hex(42) AS result
```

The query returns the following result.


| result | 
| --- | 
| `2A` | 

The following SQL query uses the `to_hex` alias on a string, and converts the same text as a `BLOB`. Both return the hexadecimal digits of the bytes `H`, `e`, `l`, `l`, `o`.

```sql
SELECT
    to_hex('Hello') AS from_string,
    hex(CAST('Hello' AS BLOB)) AS from_blob
```

The query returns the following result.


| from\_string | from\_blob | 
| --- | --- | 
| `48656C6C6F` | `48656C6C6F` | 

The following SQL query converts a negative integer. The result is the integer's 64-bit two's complement form.

```sql
SELECT hex(-1) AS result
```

The query returns the following result.


| result | 
| --- | 
| `FFFFFFFFFFFFFFFF` | 

The following SQL query passes a `DOUBLE` value. The value is rounded to the integer `4` before conversion.

```sql
SELECT hex(CAST(3.7 AS DOUBLE)) AS result
```

The query returns the following result.


| result | 
| --- | 
| `4` | 

### Considerations
<a name="athena-duckdb-functions-binary-hex-considerations"></a>

A negative `BIGINT` value, or a negative value of a smaller integer type such as `INTEGER`, is shown as the 16 digits of its 64-bit two's complement form. For example, `hex(-1)` returns `FFFFFFFFFFFFFFFF`. A negative `HUGEINT` value is shown as the 32 digits of its 128-bit two's complement form, and a negative `BIGNUM` value is shown with a minus sign.

A `DOUBLE` or `FLOAT` value is rounded to the nearest integer first, so `hex` does not show the bits of a floating-point number. A negative `DOUBLE` or `FLOAT` value is shown with a minus sign, not in two's complement form, so `hex(CAST(-1.0 AS DOUBLE))` returns `-1`. A `DECIMAL` value, such as the literal `3.7`, makes the query fail. Cast it to an integer type first.

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-binary-hex-differences-from-engine-version-3"></a>

In Athena engine version 3, `to_hex` takes a `VARBINARY` value, so a query converts a string first, as in `to_hex(to_utf8({{string}}))`. DuckDB does not have `to_utf8`. Pass the string to `to_hex` directly, as in `to_hex({{string}})`, which returns the same uppercase digits for the string's UTF-8 bytes.

## `octet_length` function
<a name="athena-duckdb-functions-binary-octet-length"></a>

Returns the size of `value` in bytes. For a `BLOB`, the size is the number of bytes that it holds. For a `BIT` value, it is the number of bytes that hold its bits: the number of bits divided by 8, rounded up. A bitstring of 7 bits returns `1`, and one of 9 bits returns `2`.

If `value` is `NULL`, the result is `NULL`.

### Syntax
<a name="athena-duckdb-functions-binary-octet-length-syntax"></a>

```sql
octet_length({{value}})
```

### Arguments
<a name="athena-duckdb-functions-binary-octet-length-arguments"></a>

The `octet_length` function takes the following argument:

**{{value}}**  
The value to measure, as a `BLOB` or `BIT` value.

### Return type
<a name="athena-duckdb-functions-binary-octet-length-return-type"></a>

`BIGINT`

### Examples
<a name="athena-duckdb-functions-binary-octet-length-examples"></a>

The following SQL query returns the number of bytes in a `BLOB`.

```sql
SELECT octet_length('\xAA\xBB'::BLOB) AS result
```

The query returns the following result.


| result | 
| --- | 
| `2` | 

The following SQL query returns the number of bytes that a 7-bit bitstring takes. It writes the type as `BITSTRING`, which is another name for `BIT`.

```sql
SELECT octet_length('1101011'::BITSTRING) AS result
```

The query returns the following result.


| result | 
| --- | 
| `1` | 

The following SQL query compares the bytes and the characters of a string that ends with the letter e with an acute accent, `chr(233)`. That letter is two bytes in UTF-8.

```sql
SELECT
    octet_length(encode('caf' || chr(233))) AS bytes,
    length('caf' || chr(233)) AS characters
```

The query returns the following result.


| bytes | characters | 
| --- | --- | 
| `5` | `4` | 

### Considerations
<a name="athena-duckdb-functions-binary-octet-length-considerations"></a>

`octet_length` does not accept `VARCHAR` values, and a string literal or a `NULL` literal with no type fails because it could be either a `BLOB` or a `BIT` value. To count the bytes of a string, pass its UTF-8 bytes, as in `octet_length(encode({{string}}))`. To count its characters, use the [`length` function](duckdb-functions-list-length-and-elements.md#athena-duckdb-functions-list-length).

`length` does not accept a `BLOB` value. Use `octet_length` instead.

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-binary-octet-length-differences-from-engine-version-3"></a>

In Athena engine version 3, `length` returns the number of bytes in a `VARBINARY` value. In DuckDB, `length` does not accept a `BLOB` value, so use `octet_length` instead.

## `repeat` function
<a name="athena-duckdb-functions-binary-repeat"></a>

Returns `value` repeated `count` times. The scalar form joins the copies into one value of the same type: one string for a `VARCHAR`, one blob for a `BLOB`, and one longer list for a list. If `count` is `0` or negative, the result is empty: an empty string, an empty blob, or an empty list. If any argument is `NULL`, the result is `NULL`.

The table form, which you use in the `FROM` clause, returns `count` rows, each with `value` in its one column.

### Syntax
<a name="athena-duckdb-functions-binary-repeat-syntax"></a>

```sql
repeat({{value}}, {{count}})
FROM repeat({{value}}, {{count}})
    [ AS {{table_alias}}({{column_alias}}) ]
```

### Arguments
<a name="athena-duckdb-functions-binary-repeat-arguments"></a>

The `repeat` function takes the following arguments:

**{{value}}**  
The value to repeat. In the scalar form, it is a `VARCHAR`, `BLOB`, or list value. In the table form, it is a constant of any type to return in each row.

**{{count}}**  
The number of repeats in the scalar form, or the number of rows in the table form, as a `BIGINT`.

### Return type
<a name="athena-duckdb-functions-binary-repeat-return-type"></a>

In the scalar form, the type of `value`: `VARCHAR`, `BLOB`, or a list of the same element type, such as `INTEGER[]`. In the table form, a table with one column of the type of `value`.

### Examples
<a name="athena-duckdb-functions-binary-repeat-examples"></a>

The following SQL query repeats a string five times.

```sql
SELECT repeat('A', 5) AS result
```

The query returns the following result.


| result | 
| --- | 
| `AAAAA` | 

The following SQL query repeats a `BLOB` of two bytes five times.

```sql
SELECT repeat('\xAA\xBB'::BLOB, 5) AS result
```

The query returns the following result.


| result | 
| --- | 
| `\xAA\xBB\xAA\xBB\xAA\xBB\xAA\xBB\xAA\xBB` | 

The following SQL query repeats the elements of a list three times.

```sql
SELECT repeat([1, 2], 3) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[1, 2, 1, 2, 1, 2]` | 

The following SQL query uses the table form to return a string in three rows, and numbers the rows.

```sql
SELECT row_number() OVER () AS n, value
FROM repeat('hello', 3) AS r(value)
ORDER BY n
```

The query returns the following results.


| n | value | 
| --- | --- | 
| `1` | `hello` | 
| `2` | `hello` | 
| `3` | `hello` | 

### Considerations
<a name="athena-duckdb-functions-binary-repeat-considerations"></a>

The scalar form does not accept numbers. To repeat a number as text, cast it to `VARCHAR` first. To build a list that holds one value `count` times, wrap the value in a list, as in `repeat([{{value}}], {{count}})`.

In the table form, `value` and `count` must be constants, so you cannot pass a column from another table in the same `FROM` clause. A negative or `NULL` `count` fails the query.

In the table form, the column is named after the value, for example `hello` for `repeat('hello', 3)`. To give the column a name of your own, use a table alias with a column list, as in `repeat('hello', 3) AS r(value)`.

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-binary-repeat-differences-from-engine-version-3"></a>

In Athena engine version 3, `repeat({{element}}, {{count}})` returns an array that holds `element` `count` times. In DuckDB, `repeat` of a string returns one string, the input repeated `count` times. To build a list that holds a value `count` times, wrap the value in a list, as in `repeat([{{element}}], {{count}})`.

## `to_base64` function
<a name="athena-duckdb-functions-binary-to-base64"></a>

Returns the base64 encoding of `blob` as a `VARCHAR` value. To encode text, convert it to a `BLOB` first with `encode`, which uses the UTF-8 bytes of the text. To decode a base64 string, use the [`from_base64` function](#athena-duckdb-functions-binary-from-base64).

`base64` is an alias of `to_base64`.

### Syntax
<a name="athena-duckdb-functions-binary-to-base64-syntax"></a>

```sql
to_base64({{blob}})
```

### Arguments
<a name="athena-duckdb-functions-binary-to-base64-arguments"></a>

The `to_base64` function takes the following argument:

**{{blob}}**  
The bytes to encode, as a `BLOB` value.

### Return type
<a name="athena-duckdb-functions-binary-to-base64-return-type"></a>

`VARCHAR`

### Examples
<a name="athena-duckdb-functions-binary-to-base64-examples"></a>

The following SQL query encodes the UTF-8 bytes of the text `hello`.

```sql
SELECT to_base64(encode('hello')) AS result
```

The query returns the following result.


| result | 
| --- | 
| `aGVsbG8=` | 

The following SQL query uses the `base64` alias to encode a `BLOB` that holds one byte, the ASCII code for the letter `A`.

```sql
SELECT base64('A'::BLOB) AS result
```

The query returns the following result.


| result | 
| --- | 
| `QQ==` | 

### Considerations
<a name="athena-duckdb-functions-binary-to-base64-considerations"></a>

`to_base64` accepts only `BLOB` values, and a `VARCHAR` column does not convert to `BLOB` automatically. To encode a text column, wrap it in `encode`, as in `to_base64(encode({{column}}))`.

## `unbin` function
<a name="athena-duckdb-functions-binary-unbin"></a>

Returns the bytes that `value` spells out in binary digits, with eight digits for each byte. For example, `01000001` is the byte for the letter `A`. When the number of digits is not a multiple of eight, the first byte takes the leftover digits, padded on the left with zeros.

An empty string returns an empty `BLOB`, not `NULL`. If `value` is `NULL`, the result is `NULL`. `from_binary` is an alias of `unbin`.

### Syntax
<a name="athena-duckdb-functions-binary-unbin-syntax"></a>

```sql
unbin({{value}})
```

### Arguments
<a name="athena-duckdb-functions-binary-unbin-arguments"></a>

The `unbin` function takes the following argument:

**{{value}}**  
The binary digits to decode, as a `VARCHAR` value that contains only the digits `0` and `1`.

### Return type
<a name="athena-duckdb-functions-binary-unbin-return-type"></a>

`BLOB`

### Examples
<a name="athena-duckdb-functions-binary-unbin-examples"></a>

The following SQL query converts 16 binary digits to two bytes. The bytes are the ASCII codes for `A` and `B`, so the result is shown as `AB`.

```sql
SELECT unbin('0100000101000010') AS result
```

The query returns the following result.


| result | 
| --- | 
| `AB` | 

The following SQL query uses the `from_binary` alias. The result is one byte with the value 6. That byte is not a printable character, so it is shown as an escape sequence.

```sql
SELECT from_binary('0110') AS result
```

The query returns the following result.


| result | 
| --- | 
| `\x06` | 

The following SQL query passes nine digits and uses `octet_length` to count the bytes in the result. The first byte takes the leftover digit, so the result has two bytes.

```sql
SELECT octet_length(unbin('100000001')) AS result
```

The query returns the following result.


| result | 
| --- | 
| `2` | 

### Considerations
<a name="athena-duckdb-functions-binary-unbin-considerations"></a>

Any character other than `0` or `1` makes the query fail.

## `unhex` function
<a name="athena-duckdb-functions-binary-unhex"></a>

Returns the bytes that `value` spells out in hexadecimal digits, with two digits for each byte. The digits can be uppercase or lowercase. When the number of digits is odd, the first byte takes the single leftover digit.

An empty string returns an empty `BLOB`, not `NULL`. If `value` is `NULL`, the result is `NULL`. To turn the bytes into text, use the [`decode` function](#athena-duckdb-functions-binary-decode). To convert a value to hexadecimal digits, use the [`hex` function](#athena-duckdb-functions-binary-hex). `from_hex` is an alias of `unhex`.

### Syntax
<a name="athena-duckdb-functions-binary-unhex-syntax"></a>

```sql
unhex({{value}})
```

### Arguments
<a name="athena-duckdb-functions-binary-unhex-arguments"></a>

The `unhex` function takes the following argument:

**{{value}}**  
The hexadecimal digits to decode, as a `VARCHAR` value. It can contain only hexadecimal digits: `0` to `9`, and `A` to `F` in either case.

### Return type
<a name="athena-duckdb-functions-binary-unhex-return-type"></a>

`BLOB`

### Examples
<a name="athena-duckdb-functions-binary-unhex-examples"></a>

The following SQL query converts the hexadecimal digits `2A` to one byte. That byte is the ASCII code for an asterisk, so the result is shown as `*`.

```sql
SELECT unhex('2A') AS result
```

The query returns the following result.


| result | 
| --- | 
| `*` | 

The following SQL query uses the `from_hex` alias with lowercase digits, and converts the resulting bytes to text with `decode`.

```sql
SELECT decode(from_hex('48656c6c6f')) AS result
```

The query returns the following result.


| result | 
| --- | 
| `Hello` | 

### Considerations
<a name="athena-duckdb-functions-binary-unhex-considerations"></a>

Any character that is not a hexadecimal digit makes the query fail, including the `x` of a `0x` prefix. To convert a value that has the prefix, remove it first, for example with `unhex(substring({{value}}, 3))`.