MySQL has two functions namely LPAD() and RPAD() with the help of which we can pad a string with another string.
LPAD() function, as the name suggests, left pads a string with another string. Following is the syntax for using it in MySQL
Syntax
LPAD(original_string, @length, pad_string)
Here,
- original_string is the string in which we pad another string.
- @length is the total length of the string returned after padding.
- Pad_string is the string which is to be padded with original_string.
Example
mysql> Select LPAD('My name is Ram',22,'* '); +--------------------------------+ | LPAD('My name is Ram',22,'* ') | +--------------------------------+ | * * * * My name is Ram | +--------------------------------+ 1 row in set (0.00 sec)
RPAD() function, as the name suggests, right pads a string with another string. Following is the syntax for using it in MySQL.
Syntax
RPAD(original_string, @length, pad_string)
Here,
- original_string is the string in which we pad another string.
- @length is the total length of the string returned after padding.
- Pad_string is the string which is to be padded with original_string.
Example
mysql> Select RPAD('My name is Ram',22,'* '); +--------------------------------+ | RPAD('My name is Ram',22,'* ') | +--------------------------------+ | My name is Ram* * * * | +--------------------------------+ 1 row in set (0.00 sec)