How can we stuff a string with another one using MySQL functions?

MySQLMySQLi Database

MySQL have two functions namely LPAD() and RPAD() with the help of which we can stuff a string with another string.

LPAD() function, as the name suggests, left stuff 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 stuff another string.
  • @length is the total length of string returned after stuffing.
  • Pad_string is the string which is to be stuffed with original_string.

Example

mysql> SELECT LPAD('tutorialspoint',18,'www.');
+----------------------------------+
| LPAD('tutorialspoint',18,'www.') |
+----------------------------------+
| www.tutorialspoint               |
+----------------------------------+
1 row in set (0.00 sec)

RPAD() function, as the name suggests, right stuff 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 stuff another string.
  • @length is the total length of string returned after stuffing.
  • Pad_string is the string which is to be stuffed with original_string.

Example

mysql> SELECT RPAD('www.tutorialspoint',22,'.com');
+--------------------------------------+
| RPAD('www.tutorialspoint',22,'.com') |
+--------------------------------------+
| www.tutorialspoint.com               |
+--------------------------------------+
1 row in set (0.06 sec)
raja
Published on 20-Feb-2018 18:42:36
Advertisements