MariaDB Tutorial MariaDB Advanced MariaDB Database Account Management MariaDB References

MariaDB RPAD() Function



The MariaDB RPAD() function returns a string that is right-padded with a specified string to a certain length. If the string is longer than length, this function will remove characters from the string to shorten it to the length characters.

Syntax

RPAD(string, length, pad_string)

Parameters

string Required. Specify the string to right-pad.
length Required. Specify the length of the result after the string has been right-padded.
pad_string Optional. Specify the string to right-pad to the string. If omitted, this function pads spaces.

Return Value

Returns a string that is right-padded with a specified string to a certain length.

Example 1:

The example below shows the usage of RPAD() function.

SELECT RPAD('alphacodingskills', 21);
Result: 'alphacodingskills    '

SELECT RPAD('alphacodingskills', 21, ' ');
Result: 'alphacodingskills    '

SELECT RPAD('alphacodingskills', 21, '*');
Result: 'alphacodingskills****'

SELECT RPAD('alphacodingskills', 21, 'XYZ');
Result: 'alphacodingskillsXYZX'

SELECT RPAD('', 8, 'XYZ');
Result: 'XYZXYZXY'

SELECT RPAD('abc', 8, 'XYZ');
Result: 'abcXYZXY'

SELECT RPAD('alphacodingskills', 11, 'XYZ');
Result: 'alphacoding'

Example 2:

Consider a database table called Employee with the following records:

EmpIDNameCitySalary
1JohnLondon3000
2MarryNew York2750
3JoParis2800
4KimAmsterdam3100
5RameshNew Delhi3000
6HuangBeijing2800

The below mentioned query is used to right-pad the records of EmpID column of the Employee table:

UPDATE Employee SET EmpID = RPAD(EmpID, 4, 'FIN');

-- see the result
SELECT * FROM Employee;

This will produce the following result:

EmpIDNameCitySalary
1FINJohnLondon3000
2FINMarryNew York2750
3FINJoParis2800
4FINKimAmsterdam3100
5FINRameshNew Delhi3000
6FINHuangBeijing2800

❮ MariaDB Functions