MariaDB RPAD() Function
The MariaDB RPAD()
function right pads the given string to the specified length.
If you want to left pad a string, use the LPAD()
function.
MariaDB RPAD()
Syntax
Here is the syntax of the MariaDB RPAD()
function:
RPAD(str, len[, padstr])
Parameters
str
-
Required. The string to be padded.
len
-
Required. The length to reach.
padstr
-
Optional. The string to be used to right pad the original string. The default is a space.
Return value
The RPAD()
function right pads the specified string to the specified length and returns the padded string.
If len
is less than the length of the original string str
, str
will be truncated to the length of len
.
If len
is negative, the RPAD()
function will return NULL
.
If any of the arguments is NULL
, the RPAD()
function will return NULL
.
MariaDB RPAD()
Examples
Basic example
This statement shows the basic usage of the MariaDB RPAD()
function:
SELECT
RPAD('oh', 10),
RPAD('oh', 10, 'h'),
RPAD('oh', 1, 'h'),
RPAD('oh', -1, 'h'),
RPAD('Hello', 13, 'World'),
RPAD('Hello', 13, NULL)\G
Output:
RPAD('oh', 10): oh
RPAD('oh', 10, 'h'): ohhhhhhhhh
RPAD('oh', 1, 'h'): o
RPAD('oh', -1, 'h'): NULL
RPAD('Hello', 13, 'World'): HelloWorldWor
RPAD('Hello', 13, NULL): NULL
Oracle mode
In Oracle mode, MariaDB RPAD()
returns NULL
instead of an empty string.
In the default default mode, the following statement returns an empty string:
SELECT RPAD('', 0);
Output:
+-------------+
| RPAD('', 0) |
+-------------+
| |
+-------------+
Now let’s switch to Oracle mode:
SET SQL_MODE=ORACLE;
and run the code again:
SELECT RPAD('', 0);
Output:
+-------------+
| RPAD('', 0) |
+-------------+
| NULL |
+-------------+
Conclusion
The MariaDB RPAD()
function right pads the specified string to the specified length.