Skip to main content

FORMAT

Description

The FORMAT function returns a string formatted using the specified format string and parameters. The formatting rules follow the fmt format specification.

Syntax

FORMAT(<format>, <args>[, ...])

Parameters

ParameterDescription
<format>Format string containing format placeholders. Type: VARCHAR
<args>Parameters to be formatted (can be multiple). Type: ANY

Return Value

Returns VARCHAR type, representing the result formatted according to the format string.

Special cases:

  • If any parameter is NULL, returns NULL
  • Format string uses {} as placeholder
  • Supports positional parameters (e.g., {0}, {1}) and named parameters
  • Supports various format options (alignment, precision, padding, etc.)

Examples

  1. Basic usage: Format number precision
SELECT format('{:.2}', pi());
+-----------------------+
| format('{:.2}', pi()) |
+-----------------------+
| 3.1 |
+-----------------------+
  1. Multiple parameter formatting
SELECT format('{0}-{1}', 'hello', 'world');
+-------------------------------------+
| format('{0}-{1}', 'hello', 'world') |
+-------------------------------------+
| hello-world |
+-------------------------------------+
  1. Alignment and padding
SELECT format('{:>10}', 123);
+-----------------------+
| format('{:>10}', 123) |
+-----------------------+
| 123 |
+-----------------------+
  1. NULL value handling
SELECT format('{:.2}', NULL);
+-----------------------+
| format('{:.2}', NULL) |
+-----------------------+
| NULL |
+-----------------------+
  1. UTF-8 string handling
SELECT format('{0}-{1}', 'ṭṛṭṛ', 'ṭṛ');
+---------------------------------------------+
| format('{0}-{1}', 'ṭṛṭṛ', 'ṭṛ') |
+---------------------------------------------+
| ṭṛṭṛ-ṭṛ |
+---------------------------------------------+