Oracle RPAD Function is used to pad a string with a set of characters from the right-side. SQL RPAD Function can not only pad a string with spaces but also with characters supplied as arguments. Syntax and examples of PLSQL RPAD Function are mentioned below.
Oracle RPAD Function Syntax
SELECT RPAD(string_1,padded_length[,padding_string]) FROM table_name;
In the above PLSQL RPAD Function Syntax:-
- string_1 is the string which will be padded from the right side. The string_1 should not be NULL.
- padded_length is the number of characters in return, if the padded_length is smaller than string_1, then RPAD function will pad spaces to the right side of string_1.
Oracle RPAD Function – Use with SQL SELECT Statement Example
SQL RPAD Function is used with the SQL SELECT Statement.
For example, the PLSQL RPAD Function query below returns string padded with spaces on the right side.
SELECT RPAD('Tech', 7) FROM dual;
The above PLSQL RPAD Function query returns ‘Tech ‘.
Oracle RPAD Function – Padding with Spaces Example
SQL RPAD Function can pad a string with white spaces on the right side.
For example, the SQL RPAD Function query below returns string padded with spaces on the right side.
SELECT RPAD('Tech', 7) FROM dual;
The above SQL RPAD Function query returns ‘Tech ‘.
Oracle RPAD Function – Padding with Characters Example
SQL RPAD Function can also pad a string with any character.
For example, the SQL RPAD Function query below returns the string padded witg ‘-‘ on the right side.
SELECT RPAD('Honey', 8, '-') FROM dual;
The above SQL RPAD Function query returns ‘Honey- – -‘
Oracle RPAD Function – Using Exact Length of String Example
If we try to pad a string with the same length as that of the string, then the Oracle RPAD Function returns the string.
For example, the SQL RPAD Function query below tries to pad a string of length 10.
SELECT RPAD('Tech Honey', 10, '-') FROM dual;
The above SQL RPAD Function query returns ‘Tech Honey’.
Notice that there is no change in passed and returned string as there was no scope of padding for SQL RPAD Function.