RPAD()
SQL · String Functions
Theory
RPAD(string, length, pad) adds the pad character(s) to the right of a string until it reaches the target length. RPAD('IT', 5, '-') returns 'IT---'. Like LPAD, a string that is already longer than length is cut down to length, so RPAD('Marketing', 5, '.') returns 'Marke'. The pad can be more than one character and is repeated as needed.
RPAD is available in MySQL, Postgres, Oracle and Snowflake, but not in SQL Server or SQLite. In those you append enough padding and then cut to the length you want: SUBSTR(dept || '........', 1, 8) in SQLite, or LEFT(dept + REPLICATE('.', 8), 8) in SQL Server. Here the emulation matches RPAD exactly, because both keep the left part. Typical uses are fixed-width text files (each field padded to an exact column width), aligned plain-text reports, and dot leaders such as 'Sales......'.
Why we use it
Some systems still exchange fixed-width files where every field must be exactly n characters, and plain-text reports need aligned columns. RPAD makes each value exactly the right width so columns line up.
When to use it
Use RPAD for fixed-width exports, aligned text output and dot leaders. Set the length to fit your longest value, or it will be cut. For screen display in BI tools, alignment is better done in the tool than in SQL.
How it works
- Take the string, target length and pad character
- Is the string longer than the target? Yes: Cut it to the target length. No: Continue.
- Count how many characters are missing
- Repeat the pad on the right to fill the gap
- Return the fixed-width string
Syntax
RPAD(string, length, pad_char)Example
SELECT RPAD('IT', 5, '-');
-- 'IT---'Key points
- Useful for fixed-width text files and aligned reports
- Not available in SQLite or SQL Server: append padding then cut
- Values longer than the target length are truncated
- The pad string is repeated as many times as needed
- Pick a length that fits your longest value