Knowledge base / SQL
SQL RPAD: right padding, target width and truncation
I explain how to pad text to a target width and why a small limit can silently remove the end of a value.

RPAD appends characters on the right until a string reaches a requested result length. I use it for presentation or exports with fixed-width fields. The main risk appears when the input already exceeds the limit: the function can truncate it while the query continues to produce a tidy-looking result.
These examples use synthetic data and simple ASCII characters. The main expressions target PostgreSQL and MySQL 8.4, with Oracle 19c syntax labelled separately. I checked the documentation on 22 November 2026. Character count, display width and encoded byte length need to be treated as distinct requirements.
The length covers the entire result
The basic form is RPAD(text, length, fill). The arguments supply the input text, target length and padding pattern. SQL contains three characters, so a target of seven requires four dots. This PostgreSQL and MySQL example returns SQL.....
SELECT RPAD('SQL', 7, '.') AS padded;
It does not append seven additional dots. The length includes the original SQL. For a fixed-width field, I define its full width and let the function supply the missing part. I do not calculate that width from the appearance of text in a proportional font, where equally long strings can occupy different screen space.
PostgreSQL and MySQL document both padding and shortening of overlong input. Dots make the small example visible. When a specification requires spaces, use ' ', remembering that some SQL clients and presentation formats do not clearly show trailing spaces.
A padding pattern can contain several characters
The fill argument need not be a single character. With ab, the pattern repeats until the result reaches its target. SQL has three characters and the target is eight, leaving five characters to add: ababa. The following expression therefore returns SQLababa.
SELECT RPAD('SQL', 8, 'ab') AS padded;
The final repetition can be partial. Do not assume that the output always ends with the complete pattern. For a technical export, I usually choose one padding character because its meaning is easier to agree with the recipient. A decorative pattern can help demonstrate the function, but should not accidentally become a data-exchange format.
Overlong text is truncated
SELECT RPAD('ABCDE', 3, '.') AS padded;
The result is ABC. RPAD does more than append: it enforces the target length even when that removes part of the input. Before exporting, establish whether truncation is permitted. A shortened product code or identifier can refer to another record or become indistinguishable from a different source value.
For a mandatory field, I prefer to stop or flag an overlong record rather than silently accept the truncated value. A neat alignment does not establish completeness. A suitable character-length function can support a separate width check; see CHAR_LENGTH for the distinction between characters and bytes.
The third argument depends on the dialect
PostgreSQL permits the fill argument to be omitted and defaults to a space. Oracle 19c documents the same default. MySQL 8.4 requires the third argument. The two-argument expression is therefore not a universally portable SQL form.
-- PostgreSQL
SELECT RPAD('SQL', 7);
-- Oracle 19c
SELECT RPAD('SQL', 7) FROM DUAL;
-- MySQL 8.4
SELECT RPAD('SQL', 7, ' ');
Each labelled variant produces SQL followed by four spaces. Supplying the space explicitly also works as a clear statement of intent in PostgreSQL and Oracle. A reviewer can then see the chosen padding character immediately rather than reconstructing the default behaviour of a particular engine.

Keep the report-line dialect explicit
Suppose an ASCII text code occupies six positions and a description occupies five. Dots stand in for spaces only to make the output visible. In PostgreSQL I join the fields with ||.
-- PostgreSQL
SELECT RPAD('A1', 6, '.') || RPAD('BOX', 5, '.') AS line;
The result is A1....BOX.., containing eleven characters. The first field contributes two content characters and four dots; the second contributes three content characters and two dots. For this simple Oracle 19c variant, add FROM DUAL. In MySQL 8.4 I state concatenation explicitly with CONCAT.
-- MySQL 8.4
SELECT CONCAT(RPAD('A1', 6, '.'), RPAD('BOX', 5, '.')) AS line;
MySQL’s || operator means logical OR by default unless an applicable mode changes it. The older Polish example also mixed PostgreSQL’s ::TEXT cast into a discussion of Oracle and MySQL. I remove that misleading universal expression. Numeric formatting belongs in a separate, engine-specific step before padding the resulting text.
NULL and export width need separate rules
MySQL returns NULL when the input text, target length or fill pattern is NULL. A missing field is not automatically replaced by spaces. Oracle documents a non-null input requirement, and its empty-string behaviour has a special relationship with NULL. For mandatory fields I establish a missing-value status before formatting. If absence is permitted, the export specification should define its representation.
In Oracle, the RPAD length refers to display width, which can differ from character count in some character sets. Even ordinary character count does not guarantee UTF-8 byte length. ASCII simplifies this example, but an actual file still needs an agreed encoding, line ending and field-width specification.
Exercise before exporting
Write the expected RPAD result at length five for AB, ABCDE and ABCDEFG, using a dot. The results are AB..., ABCDE and ABCDE. In the final case, decide whether losing FG is allowed. Only then choose the handling rule for an overlong field. For joining the formatted elements, continue to CONCAT.
Key takeaways
Does the RPAD length mean the number of added characters?
No. It is the target length of the complete result. Padding SQL to length 7 adds four characters.
Can the third RPAD argument be omitted?
PostgreSQL and Oracle make it optional with a space default. MySQL 8.4 requires it.
Does RPAD preserve text longer than its target?
No. Input exceeding the target length is truncated. Define how overlong fields should be handled before exporting.
AI was used in an automated way while working on this article. If you want to learn how to do it too, check out this project.
