lpad, rpad

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

ArgumentTypeDescription
<string>stringString to pad.
widthintConstant. Non-negative target length in Unicode characters. Must not be NULL.
padstringOptional constant; defaults to ' '. Nonempty padding string. Must not be NULL.

Returns

Returns string.

  • If the input is shorter than width, repeats pad and cuts off any excess padding.
  • If the input is longer than width, keeps its first width characters.
  • 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 ]    |
+--------------+---------------+