Skip to main content

PRINTF

Formats its arguments into a string according to a C printf-style format string.

See also:
[SELECT] [CONCAT]


Syntax​

SELECT PRINTF(format [, value ...]) FROM <multipartIdentifier>;

Arguments​

format
A format string. Conversions include %d (integer), %f (floating point, for example %.2f), %s (string), %x (hexadecimal), %q (SQL-quoted string) and %% (a literal percent sign), with optional flags, width and precision such as %-30s or %08d.

value
The values substituted into the conversions, in order.

StackQL accepts PRINTF only; the FORMAT alias that SQLite also provides is not accepted by the StackQL parser.

Return Value(s)​

Returns a string.


Examples​

Format numbers​

The following query rounds to two decimal places with a literal percent sign, pads an integer with zeros and prints a number in hexadecimal:

SELECT PRINTF('%.2f%%', 12.3456) AS pct, PRINTF('%08d', 42) AS padded, PRINTF('%x', 255) AS hex;
|-----|----------|--------|
| hex | padded | pct |
|-----|----------|--------|
| ff | 00000042 | 12.35% |
|-----|----------|--------|

Align columns in a text report​

The following query pads repository names to a fixed width and right-aligns their star counts:

SELECT PRINTF('%-35s %5d stars', name, stargazers_count) AS line
FROM github.repos.repos
WHERE org = 'stackql-labs';

For more information, see https://www.sqlite.org/printf.html