Pad the input string on the right with a repeating pad sequence until it reaches a target length.
RPAD(str, len [, pad])
## Overview Returns the input string padded on the right to the specified total character length by repeating the pad sequence. If the input is already at least as long as the target length, it is truncated from the right to fit. RPAD is the standard way to align columns in fixed-width file formats such as mainframe EDI, HL7, and legacy bank feeds. When the pad argument is multi-character, RPAD concatenates as many copies as needed and then truncates the last partial copy so that the result has exactly the requested length. ## Behavior - Returns NULL when any argument is NULL. - If `len` is less than or equal to 0, returns an empty string. - If `len` is less than the character length of `str`, the input is truncated from the right to exactly `len` characters. - If `pad` is an empty string, the input is returned unchanged. - Operates on Unicode code points; multi-byte characters count as one position. - The pad sequence is repeated starting at the right of the input; the last repetition is truncated if it would overshoot. ## Compatibility - Widely supported with matching semantics. - Treats the target length as a character count, not a byte count.
| Name | Type | Description |
|---|---|---|
str | Specifies the input string to pad. | |
len | Specifies the desired total character length of the result. If the input is longer than this, it is truncated from the right. | |
pad | Specifies the pad string. If multi-character, it is repeated and then trimmed to fit the required gap. Defaults to a single space. |
-- Pad with spaces
SELECT RPAD('hi', 5); -- 'hi '
-- Pad with a fill character
SELECT RPAD('hi', 5, '.'); -- 'hi...'
-- Truncate when target length is shorter
SELECT RPAD('hello', 3, ' '); -- 'hel'
-- Multi-character pad sequence is repeated
SELECT RPAD('x', 6, 'ab'); -- 'xababa'
-- No padding needed
SELECT RPAD('hello', 5, ' '); -- 'hello'
-- NULL propagates
SELECT RPAD(CAST(NULL AS VARCHAR), 5, ' '); -- NULL