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
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
format(format [, value, ...])
Arguments
The format function takes the following arguments:
format-
The format string, as a
VARCHARvalue 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
VARCHAR
Examples
The following SQL query puts a string and a number into a sentence.
SELECT format( 'Benchmark "{}" took {} seconds', 'CSV', 42 ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query pads a number with zeros, shows a number with two decimal places, picks arguments by position, and adds thousands separators.
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 |
|---|---|---|---|
|
|
|
|
The following SQL query passes a NULL value, so the whole result is NULL.
SELECT format('Hello, {}', NULL) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows that format leaves % patterns unchanged, and that printf reads them.
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 |
|---|---|
|
|
Considerations
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.
Differences from Athena engine version 3
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
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
format_bytes(bytes)
Arguments
The format_bytes function takes the following argument:
bytes-
The number of bytes to format, as a
BIGINTvalue.
Return type
VARCHAR
Examples
The following SQL query formats 16,000 bytes.
SELECT format_bytes(16000) AS result
The query returns the following result.
| result |
|---|
|
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.
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 |
|---|---|---|
|
|
|
The following SQL query uses the formatReadableSize alias to format 1,048,576 bytes, which is exactly 1 MiB.
SELECT formatReadableSize(1048576) AS result
The query returns the following result.
| result |
|---|
|
Considerations
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
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.
Syntax
formatReadableDecimalSize(integer)
Arguments
The formatReadableDecimalSize function takes the following argument:
integer-
The number of bytes to format, as a
BIGINTvalue.
Return type
VARCHAR
Examples
The following SQL query formats 16,000 bytes.
SELECT formatReadableDecimalSize(16000) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query formats a value under 1,000, a value that is truncated, and a value in megabytes.
SELECT formatReadableDecimalSize(999) AS under_1000, formatReadableDecimalSize(1999) AS truncated, formatReadableDecimalSize(1500000) AS megabytes
The query returns the following result.
| under_1000 | truncated | megabytes |
|---|---|---|
|
|
|
parse_formatted_bytes function
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
parse_formatted_bytes(string)
Arguments
The parse_formatted_bytes function takes the following argument:
string-
The size to convert, as a
VARCHARvalue such as'16 KiB'or'2 GB'.
Return type
UBIGINT
Examples
The following SQL query converts a size in kibibytes to bytes.
SELECT parse_formatted_bytes('16 KiB') AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows the difference between a decimal unit and a binary unit.
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 |
|---|---|
|
|
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.
SELECT parse_formatted_bytes(format_bytes(16000)) AS result
The query returns the following result.
| result |
|---|
|
Considerations
The query fails if string has no unit, uses a unit larger than TB or TiB, or holds a negative number.
printf function
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.
Syntax
printf(format [, value, ...])
Arguments
The printf function takes the following arguments:
format-
The format string that holds the text and the format specifiers, as a
VARCHARvalue. value-
The values to format, one for each specifier, in order. You can pass any number of values. This argument is optional.
Return type
VARCHAR
Examples
The following SQL query puts a string and an integer into a sentence.
SELECT printf( 'Benchmark "%s" took %d seconds', 'CSV', 42 ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query pads an integer with zeros to three digits and rounds a decimal number to two decimal places.
SELECT printf('%s-%03d', 'x', 7) AS padded, printf('%.2f', 3.14159) AS rounded
The query returns the following result.
| padded | rounded |
|---|---|
|
|
The following SQL query writes a literal percent sign with %%.
SELECT printf('%d%%', 42) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows that one NULL value makes the whole result NULL.
SELECT printf('%s and %s', 'a', NULL) IS NULL AS is_null
The query returns the following result.
| is_null |
|---|
|
Considerations
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
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
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
to_base(number, radix [, min_length ])
Arguments
The to_base function takes the following arguments:
number-
The number to convert, as a
BIGINTvalue. It must be0or greater. radix-
The base to use, as an
INTEGERvalue from2to36. min_length-
The minimum number of characters in the result, as an
INTEGERvalue. This argument is optional.
Return type
VARCHAR
Examples
The following SQL query converts 42 to base 16 and pads the result to five characters.
SELECT to_base(42, 16, 5) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query converts 10 to base 2.
SELECT to_base(10, 2) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query returns lowercase hexadecimal digits by wrapping the call in lower.
SELECT lower(to_base(255, 16)) AS result
The query returns the following result.
| result |
|---|
|
Considerations
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
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.