lpad, rpad
On this page
Pads a string on the left or right to a specified character length.
Syntax
lpad(<string>, width => <width> [, pad => <pad>])
rpad(<string>, width => <width> [, pad => <pad>])Arguments
| Argument | Type | Description |
|---|---|---|
<string> | string | String to pad. |
width | int | Constant. Non-negative target length in Unicode characters. Must not be NULL. |
pad | string | Optional constant; defaults to ' '. Nonempty padding string. Must not be NULL. |
Returns
Returns string.
- If the input is shorter than
width, repeatspadand cuts off any excess padding. - If the input is longer than
width, keeps its firstwidthcharacters. - A width of 0 returns an empty string.
- A NULL input returns NULL.
Examples
Pad store codes on the left and status labels on the right. Brackets make the right padding visible.
VALUES ('7', 'paid'), ('42', 'pending')
SELECT $0 AS store_code, $1 AS status
SELECT
lpad(store_code, width => 4, pad => '0') AS export_store,
'[' || rpad(status, width => 8) || ']' AS export_status+--------------+---------------+
| export_store | export_status |
+--------------+---------------+
| 0007 | [paid ] |
| 0042 | [pending ] |
+--------------+---------------+