Stuff a String with Another Using MySQL Functions



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)
Updated on: 2020-06-20T13:05:02+05:30

321 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements