In MySQL, how can we pad a string with another string?


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)

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 06-Feb-2020

85 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements