> ## Documentation Index
> Fetch the complete documentation index at: https://docs.e6data.com/llms.txt
> Use this file to discover all available pages before exploring further.

# DECODE

> Performs NULL-safe value selection or decodes bytes using a character set.

<div className="e6-meta"><span className="e6-eyebrow e6-eyebrow--quiet">Scalar function</span></div>

NULL matches a NULL search value. No match and no default returns NULL.

## Syntax

### Conditional (scalar)

```sql theme={"theme":{"light":"github-light","dark":"github-dark"}}
decode(value, search1, result1 [, search_n, result_n ...] [, default_value])
```

* All result alternatives must have compatible types. Matching types are preserved; mixed types may be converted to a common type. Use explicit casts to choose the result type.

### Charset (scalar)

```sql theme={"theme":{"light":"github-light","dark":"github-dark"}}
decode(binary_value, charset)
```

## Arguments

### Conditional

| Name | Type | Required | Description |
| - | - | - | - |
| value | BOOLEAN, TINYINT, SMALLINT, INTEGER, BIGINT, FLOAT, DOUBLE, DECIMAL(p, s), STRING, BINARY, DATE, TIME, TIMESTAMP, ARRAY, MAP or STRUCT | Yes | Value to match. For DECIMAL(p, s), p is precision (total digits) and s is scale (digits after the decimal point). |
| search1 | BOOLEAN, TINYINT, SMALLINT, INTEGER, BIGINT, FLOAT, DOUBLE, DECIMAL(p, s), STRING, BINARY, DATE, TIME, TIMESTAMP, ARRAY, MAP or STRUCT | Yes | First search value. Must convert to the same SQL type as the other arguments in this comparison or result branch. Use explicit CAST when mixing types. For DECIMAL(p, s), p is precision (total digits) and s is scale (digits after the decimal point). |
| result1 | BOOLEAN, TINYINT, SMALLINT, INTEGER, BIGINT, FLOAT, DOUBLE, DECIMAL(p, s), STRING, BINARY, DATE, TIME, TIMESTAMP, ARRAY, MAP or STRUCT | Yes | First result value. Must convert to the same SQL type as the other arguments in this comparison or result branch. Use explicit CAST when mixing types. For DECIMAL(p, s), p is precision (total digits) and s is scale (digits after the decimal point). |
| search\_n | BOOLEAN, TINYINT, SMALLINT, INTEGER, BIGINT, FLOAT, DOUBLE, DECIMAL(p, s), STRING, BINARY, DATE, TIME, TIMESTAMP, ARRAY, MAP or STRUCT | No | Additional search/result pairs. Must convert to the same SQL type as the other arguments in this comparison or result branch. Use explicit CAST when mixing types. For DECIMAL(p, s), p is precision (total digits) and s is scale (digits after the decimal point). This argument can repeat. |
| result\_n | BOOLEAN, TINYINT, SMALLINT, INTEGER, BIGINT, FLOAT, DOUBLE, DECIMAL(p, s), STRING, BINARY, DATE, TIME, TIMESTAMP, ARRAY, MAP or STRUCT | No | Result paired with search\_n. Must convert to the same SQL type as the other arguments in this comparison or result branch. Use explicit CAST when mixing types. For DECIMAL(p, s), p is precision (total digits) and s is scale (digits after the decimal point). This argument can repeat. |
| default\_value | BOOLEAN, TINYINT, SMALLINT, INTEGER, BIGINT, FLOAT, DOUBLE, DECIMAL(p, s), STRING, BINARY, DATE, TIME, TIMESTAMP, ARRAY, MAP or STRUCT | No | Value used when no search matches. Must convert to the same SQL type as the other arguments in this comparison or result branch. Use explicit CAST when mixing types. For DECIMAL(p, s), p is precision (total digits) and s is scale (digits after the decimal point). Default: NULL. |

### Charset

| Name | Type | Required | Description |
| - | - | - | - |
| binary\_value | BINARY | Yes | Bytes to decode. |
| charset | STRING | Yes | Supported character encoding. US-ASCII, ISO-8859-1, UTF-8, UTF-16, UTF-16BE, UTF-16LE and their supported alternative names. Must be a constant argument. |

## Returns

* Type: Common SQL type after conversion of the conditional result/default values
* NULL behavior: NULL matches a NULL search value. No match and no default returns NULL.

### Charset

* Type: STRING
* Possible values: Decoded text.
* NULL behavior: NULL bytes return NULL when charset is a valid non-NULL literal. NULL or nonconstant charset raises an error.

## Properties

| Property | Value |
| - | - |
| Deterministic | Yes |
| Supports window usage | No |
| Exact result | Partial (Type conversions can change values; text decoding can replace invalid byte sequences.) |

## Usage notes

* The first matching search/result pair wins. Matching uses NULL-safe equality.
* Two arguments select the binary/charset form; three or more arguments select conditional matching.
* INTEGER and INT mean the same type; STRING means VARCHAR text. NULL can be used with a supported type. Cast NULL explicitly when its type would otherwise be unclear.
* Query tools may report a DECIMAL result as DOUBLE or a TIME result as INTEGER. The returned value still keeps its decimal precision and scale, or its time type. A reported type does not convert the value.
* Deterministic: The same inputs and settings produce the same result.

## Examples

### Typical behavior

```sql theme={"theme":{"light":"github-light","dark":"github-dark"}}
SELECT decode(CAST(NULL AS INT), CAST(NULL AS INT), 'match', 'other') AS "value";
```

| value |
| - |
| match |

### Decode binary bytes

```sql theme={"theme":{"light":"github-light","dark":"github-dark"}}
SELECT decode(X'4869', 'UTF-8') AS "value";
```

| value |
| - |
| Hi |

## Limitations

* UTF-8 and ASCII invalid bytes are replaced with U+FFFD. UTF-16 rejects odd byte counts or invalid surrogate sequences. UTF-16 detects a BOM and otherwise defaults to big endian.
* A charset is a call argument and must be a non-NULL constant. It is not a session parameter.
* Conditional search operands and result branches must have compatible types.

## Related functions

* [if](/query/sql/functions/conditional/if)
* [nvl](/query/sql/functions/conditional/nvl)
* [encode](/query/sql/functions/string/encode)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.