SQL RPAD function
Pads a string on the right with a fill pattern until it reaches the requested length.
- Kategorie
- Strings
- Returns
string- In this category
- 114 of 172
Syntax
RPAD(str, length [, pad_string]) -- pad_string is required in MySQL
Engine-Unterstützung
| Engine | Support | Notizen |
|---|---|---|
| SQL Server | Not built in | No RPAD; LEFT(str + REPLICATE(pad, n), n). |
| PostgreSQL | Ja | Supported under this name. |
| MySQL | Ja | Supported under this name. |
| SQLite | Not built in | No RPAD; printf('%-10s', str) pads with spaces. |
| Oracle | Ja | Supported under this name. |
Beispiele
PostgreSQL
SELECT RPAD('id-', 6, '0') AS code; -- id-000
MySQL
SELECT RPAD(name, 20, '.') AS name_line FROM products;
Gut zu wissen
- A target length shorter than the input truncates it: RPAD('abcdef', 4, 'x') is 'abcd', so padding and clipping are the same call.
- The pad string repeats and is cut to fit: RPAD('ab', 8, 'xy') is 'abxyxyxy'.
- PostgreSQL and Oracle pad with spaces when the third argument is omitted; MySQL requires it. Oracle measures the length in display width, which matches characters except in some multibyte character sets.
Questions about RPAD
How do I right-pad in SQL Server?
There is no RPAD. LEFT(str + REPLICATE(pad, n), n) pads and truncates to n characters; LEFT(str + SPACE(n), n) for spaces.
Does RPAD truncate long strings?
Yes. The result is exactly length characters, so RPAD also works as a fixed-width clip. In PostgreSQL an empty pad string means no padding at all.
Is RPAD case-sensitive or collation-aware?
Neither applies: padding appends the pad pattern verbatim and never compares characters.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: PostgreSQL, MySQL, Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.