

# String formatting functions

Use these functions to turn values into formatted text, such as a number with a fixed width, a byte count in readable units, or a number in another base.

One of them works the other way, and reads a size such as `16 KiB` as a number of bytes.

## `format` function
<a name="athena-duckdb-functions-string-format"></a>

Returns `format` with each `{}` placeholder replaced by the next `value`, converted to text. In a placeholder, you can give an argument position, such as `{1}`. Positions start at `0`. You can also give a format specification after a colon. For example, `{:05d}` pads an integer with zeros to five digits, `{:.2f}` shows two decimal places, and `{:,}` adds thousands separators. To include a literal brace, double it: `{{` or `}}`.

The placeholders follow the syntax of the open-source fmt formatting library. If `format` or any `value` is `NULL`, returns `NULL`.

### Syntax
<a name="athena-duckdb-functions-string-format-syntax"></a>

```sql
format({{format}} [, {{value}}, ...])
```

### Arguments
<a name="athena-duckdb-functions-string-format-arguments"></a>

The `format` function takes the following arguments:

**{{format}}**  
The format string, as a `VARCHAR` value that contains `{}` placeholders.

**{{value}}**  
The values to put in the placeholders. You can pass zero or more values of any type. This argument is optional.

### Return type
<a name="athena-duckdb-functions-string-format-return-type"></a>

`VARCHAR`

### Examples
<a name="athena-duckdb-functions-string-format-examples"></a>

The following SQL query puts a string and a number into a sentence.

```sql
SELECT
    format(
        'Benchmark "{}" took {} seconds', 'CSV', 42
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `Benchmark "CSV" took 42 seconds` | 

The following SQL query pads a number with zeros, shows a number with two decimal places, picks arguments by position, and adds thousands separators.

```sql
SELECT
    format('{:05d}', 42) AS zero_padded,
    format('{:.2f}', 3.14159) AS two_decimals,
    format('{1}-{0}', 'a', 'b') AS by_position,
    format('{:,}', 1234567) AS with_commas
```

The query returns the following result.


| zero\_padded | two\_decimals | by\_position | with\_commas | 
| --- | --- | --- | --- | 
| `00042` | `3.14` | `b-a` | `1,234,567` | 

The following SQL query passes a `NULL` value, so the whole result is `NULL`.

```sql
SELECT format('Hello, {}', NULL) AS result
```

The query returns the following result.


| result | 
| --- | 
| `NULL` | 

The following SQL query shows that `format` leaves `%` patterns unchanged, and that `printf` reads them.

```sql
SELECT
    format('%s-%03d', 'x', 7) AS format_result,
    printf('%s-%03d', 'x', 7) AS printf_result
```

The query returns the following result.


| format\_result | printf\_result | 
| --- | --- | 
| `%s-%03d` | `x-007` | 

### Considerations
<a name="athena-duckdb-functions-string-format-considerations"></a>

A placeholder that has no matching argument causes an error. Arguments that have no placeholder are ignored.

`format` does not read `%` patterns, so a pattern such as `%s` stays in the result unchanged. For `%` patterns, use the [`printf` function](#athena-duckdb-functions-string-printf).

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

In Athena engine version 3, `format` takes `%` patterns, such as `format('%s-%03d', 'x', 7)`. In DuckDB, `format` uses `{}` placeholders and leaves `%` patterns unchanged. To use `%` patterns, call `printf`.

## `format_bytes` function
<a name="athena-duckdb-functions-string-format-bytes"></a>

Returns `bytes` as a readable size string. For a value of 1,024 bytes or more, the function divides the value by the largest power of 1,024 that fits. It shows the result with one decimal place and a binary unit, such as `KiB`, `MiB`, or `GiB`. The largest unit is `PiB`, so a value of 1,024 PiB or more is still shown in PiB, such as `1024.0 PiB`. Digits after the first decimal place are dropped, not rounded, so `2007` becomes `1.9 KiB`. For a value from 0 to 1,023, the function shows the whole number followed by `bytes`, such as `1000 bytes`, or shows `1 byte` for a value of 1. A negative value is shown in the same way as its absolute value, with a minus sign in front, so `-16000` becomes `-15.6 KiB`. If `bytes` is `NULL`, the result is `NULL`.

`formatReadableSize` is an alias of `format_bytes`. To format sizes in decimal units (powers of 1,000), use `formatReadableDecimalSize`.

### Syntax
<a name="athena-duckdb-functions-string-format-bytes-syntax"></a>

```sql
format_bytes({{bytes}})
```

### Arguments
<a name="athena-duckdb-functions-string-format-bytes-arguments"></a>

The `format_bytes` function takes the following argument:

**{{bytes}}**  
The number of bytes to format, as a `BIGINT` value.

### Return type
<a name="athena-duckdb-functions-string-format-bytes-return-type"></a>

`VARCHAR`

### Examples
<a name="athena-duckdb-functions-string-format-bytes-examples"></a>

The following SQL query formats 16,000 bytes.

```sql
SELECT format_bytes(16000) AS result
```

The query returns the following result.


| result | 
| --- | 
| `15.6 KiB` | 

The following SQL query shows that a positive value under 1,024 bytes stays in bytes, and that values of 1,024 bytes or more switch to binary units.

```sql
SELECT
    format_bytes(1000) AS small,
    format_bytes(1024) AS one_kib,
    format_bytes(5368709120) AS five_gib
```

The query returns the following result.


| small | one\_kib | five\_gib | 
| --- | --- | --- | 
| `1000 bytes` | `1.0 KiB` | `5.0 GiB` | 

The following SQL query uses the `formatReadableSize` alias to format 1,048,576 bytes, which is exactly 1 MiB.

```sql
SELECT formatReadableSize(1048576) AS result
```

The query returns the following result.


| result | 
| --- | 
| `1.0 MiB` | 

### Considerations
<a name="athena-duckdb-functions-string-format-bytes-considerations"></a>

`format_bytes` accepts `BIGINT` and smaller integer values. To format a `DECIMAL`, `DOUBLE`, or `UBIGINT` value, cast it to `BIGINT` first. Otherwise, the query fails.

## `formatReadableDecimalSize` function
<a name="athena-duckdb-functions-string-formatreadabledecimalsize"></a>

Returns `integer`, a number of bytes, as text with one digit after the decimal point and the largest decimal unit that fits: `kB`, `MB`, `GB`, `TB`, or `PB`. Each unit is 1,000 times the one before it, so 1 `kB` is 1,000 bytes. A value less than 1,000 is shown as a whole number of bytes, such as `999 bytes`.

The value is truncated, not rounded, to one decimal place, so `1999` becomes `1.9 kB`. If `integer` is `NULL`, returns `NULL`. To format sizes in binary units (powers of 1,024), such as `KiB` and `MiB`, use the [`format_bytes` function](#athena-duckdb-functions-string-format-bytes).

### Syntax
<a name="athena-duckdb-functions-string-formatreadabledecimalsize-syntax"></a>

```sql
formatReadableDecimalSize({{integer}})
```

### Arguments
<a name="athena-duckdb-functions-string-formatreadabledecimalsize-arguments"></a>

The `formatReadableDecimalSize` function takes the following argument:

**{{integer}}**  
The number of bytes to format, as a `BIGINT` value.

### Return type
<a name="athena-duckdb-functions-string-formatreadabledecimalsize-return-type"></a>

`VARCHAR`

### Examples
<a name="athena-duckdb-functions-string-formatreadabledecimalsize-examples"></a>

The following SQL query formats 16,000 bytes.

```sql
SELECT formatReadableDecimalSize(16000) AS result
```

The query returns the following result.


| result | 
| --- | 
| `16.0 kB` | 

The following SQL query formats a value under 1,000, a value that is truncated, and a value in megabytes.

```sql
SELECT
    formatReadableDecimalSize(999) AS under_1000,
    formatReadableDecimalSize(1999) AS truncated,
    formatReadableDecimalSize(1500000) AS megabytes
```

The query returns the following result.


| under\_1000 | truncated | megabytes | 
| --- | --- | --- | 
| `999 bytes` | `1.9 kB` | `1.5 MB` | 

## `parse_formatted_bytes` function
<a name="athena-duckdb-functions-string-parse-formatted-bytes"></a>

Reads `string`, a number followed by a size unit, and returns the size in bytes. The units `KiB`, `MiB`, `GiB`, and `TiB` are powers of 1,024, and `KB`, `MB`, `GB`, and `TB` are powers of 1,000. For plain bytes, use `B` or `bytes`. Units are not case-sensitive, the space before the unit is optional, and the number can have a fractional part. If `string` is `NULL`, the result is `NULL`.

The function accepts the strings that `format_bytes` returns for sizes from 0 bytes up to, but not including, 1,024 TiB. For sizes of 1,024 TiB or more, `format_bytes` uses the unit `PiB`, which this function does not accept. Because `format_bytes` truncates to one decimal place, converting a number to a string and back does not always return the original number.

### Syntax
<a name="athena-duckdb-functions-string-parse-formatted-bytes-syntax"></a>

```sql
parse_formatted_bytes({{string}})
```

### Arguments
<a name="athena-duckdb-functions-string-parse-formatted-bytes-arguments"></a>

The `parse_formatted_bytes` function takes the following argument:

**{{string}}**  
The size to convert, as a `VARCHAR` value such as `'16 KiB'` or `'2 GB'`.

### Return type
<a name="athena-duckdb-functions-string-parse-formatted-bytes-return-type"></a>

`UBIGINT`

### Examples
<a name="athena-duckdb-functions-string-parse-formatted-bytes-examples"></a>

The following SQL query converts a size in kibibytes to bytes.

```sql
SELECT parse_formatted_bytes('16 KiB') AS result
```

The query returns the following result.


| result | 
| --- | 
| `16384` | 

The following SQL query shows the difference between a decimal unit and a binary unit.

```sql
SELECT
    parse_formatted_bytes('2 GB') AS decimal_bytes,
    parse_formatted_bytes('2 GiB') AS binary_bytes
```

The query returns the following result.


| decimal\_bytes | binary\_bytes | 
| --- | --- | 
| `2000000000` | `2147483648` | 

The following SQL query converts 16,000 bytes to a string with `format_bytes` and back. `format_bytes` returns `15.6 KiB`, so the result is not exactly 16,000.

```sql
SELECT
    parse_formatted_bytes(format_bytes(16000)) AS result
```

The query returns the following result.


| result | 
| --- | 
| `15974` | 

### Considerations
<a name="athena-duckdb-functions-string-parse-formatted-bytes-considerations"></a>

The query fails if `string` has no unit, uses a unit larger than `TB` or `TiB`, or holds a negative number.

## `printf` function
<a name="athena-duckdb-functions-string-printf"></a>

Returns `format` with each format specifier replaced by the next `value`, in order. Use `%s` for strings, `%d` for integers, `%f` for floating-point and decimal numbers, and `%x` for hexadecimal. A specifier can include flags, a width, and a precision, such as `%05d` or `%.2f`. To include a literal percent sign, write `%%`.

If `format` or any `value` is `NULL`, the result is `NULL`. To format with `{}` placeholders instead of `%` specifiers, use the [`format` function](#athena-duckdb-functions-string-format).

### Syntax
<a name="athena-duckdb-functions-string-printf-syntax"></a>

```sql
printf({{format}} [, {{value}}, ...])
```

### Arguments
<a name="athena-duckdb-functions-string-printf-arguments"></a>

The `printf` function takes the following arguments:

**{{format}}**  
The format string that holds the text and the format specifiers, as a `VARCHAR` value.

**{{value}}**  
The values to format, one for each specifier, in order. You can pass any number of values. This argument is optional.

### Return type
<a name="athena-duckdb-functions-string-printf-return-type"></a>

`VARCHAR`

### Examples
<a name="athena-duckdb-functions-string-printf-examples"></a>

The following SQL query puts a string and an integer into a sentence.

```sql
SELECT
    printf(
        'Benchmark "%s" took %d seconds', 'CSV', 42
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `Benchmark "CSV" took 42 seconds` | 

The following SQL query pads an integer with zeros to three digits and rounds a decimal number to two decimal places.

```sql
SELECT
    printf('%s-%03d', 'x', 7) AS padded,
    printf('%.2f', 3.14159) AS rounded
```

The query returns the following result.


| padded | rounded | 
| --- | --- | 
| `x-007` | `3.14` | 

The following SQL query writes a literal percent sign with `%%`.

```sql
SELECT printf('%d%%', 42) AS result
```

The query returns the following result.


| result | 
| --- | 
| `42%` | 

The following SQL query shows that one `NULL` value makes the whole result `NULL`.

```sql
SELECT printf('%s and %s', 'a', NULL) IS NULL AS is_null
```

The query returns the following result.


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

### Considerations
<a name="athena-duckdb-functions-string-printf-considerations"></a>

Each specifier must suit the type of its value. A number with `%s`, such as `printf('%s', 42)`, fails, and so does a decimal number with `%d`. Use a numeric specifier, or cast the value to `VARCHAR` and use `%s`.

If `format` has more specifiers than there are values, the query fails.

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

In Athena engine version 3, `format` takes `%` specifiers such as `%s` and `%03d`. In DuckDB, `format` uses `{}` placeholders and leaves `%` specifiers in the result unchanged, without an error. Use `printf` for a format string with `%` specifiers. For example, replace `format('%s-%03d', 'x', 7)` with `printf('%s-%03d', 'x', 7)`.

In Athena engine version 3, `format` accepts `%s` for a number, as in `format('%s', 42)`. In DuckDB, `printf` fails on a number with `%s`, so use `%d` or `%f` for numbers, or cast them to `VARCHAR`.

## `to_base` function
<a name="athena-duckdb-functions-string-to-base"></a>

Returns `number` written in base `radix`, which can be from `2` to `36`. Digit values from 10 to 35 are written as the uppercase letters `A` to `Z`, so base 16 uses `0` to `9` and `A` to `F`.

With `min_length`, the result is padded on the left with zeros to at least `min_length` characters. A result that is already longer is not truncated. If any argument is `NULL`, the result is `NULL`.

### Syntax
<a name="athena-duckdb-functions-string-to-base-syntax"></a>

```sql
to_base({{number}}, {{radix}} [, {{min_length}} ])
```

### Arguments
<a name="athena-duckdb-functions-string-to-base-arguments"></a>

The `to_base` function takes the following arguments:

**{{number}}**  
The number to convert, as a `BIGINT` value. It must be `0` or greater.

**{{radix}}**  
The base to use, as an `INTEGER` value from `2` to `36`.

**{{min\_length}}**  
The minimum number of characters in the result, as an `INTEGER` value. This argument is optional.

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

`VARCHAR`

### Examples
<a name="athena-duckdb-functions-string-to-base-examples"></a>

The following SQL query converts 42 to base 16 and pads the result to five characters.

```sql
SELECT to_base(42, 16, 5) AS result
```

The query returns the following result.


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

The following SQL query converts 10 to base 2.

```sql
SELECT to_base(10, 2) AS result
```

The query returns the following result.


| result | 
| --- | 
| `1010` | 

The following SQL query returns lowercase hexadecimal digits by wrapping the call in `lower`.

```sql
SELECT lower(to_base(255, 16)) AS result
```

The query returns the following result.


| result | 
| --- | 
| `ff` | 

### Considerations
<a name="athena-duckdb-functions-string-to-base-considerations"></a>

A negative `number`, a `radix` outside the range `2` to `36`, or a `min_length` outside the range `0` to `64` makes the query fail.

`number` must be a `BIGINT` value or a smaller integer type, such as `INTEGER`. To convert a `HUGEINT`, `DECIMAL`, or `DOUBLE` value, cast it to `BIGINT` first.

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-string-to-base-differences-from-engine-version-3"></a>

In Athena engine version 3, `to_base` returns lowercase letters, for example `ff` for 255 in base 16. DuckDB returns uppercase letters. To get the Athena engine version 3 result, use `lower(to_base({{number}}, {{radix}}))`.

In Athena engine version 3, `to_base` accepts a negative number and returns a result with a leading minus sign. In DuckDB, a negative number makes the query fail.