MySQL LPAD Function

MySQL LPAD function is one of the String Function, which is useful to add (or pad) required string to the left side of the original string. For example, we can use this string LPAD in MySQL to pad dollar symbol to the left side of a Sales column, etc.

MySQL LPAD Syntax

The basic syntax of the string LPAD Function in MySQL is as shown below:

SELECT LPAD (Original_Str, length, Pad_String)
FROM [Source]
  • Original_Str: This is the actual string that is available in a column.
  • length: This is the length of a final string (after padding). If the final string is greater than this length value, then extra characters trimmed. Remember, You can use the Length function to calculate the MySQL string length.
  • Pad_String: String that you want to add to the left side of the Original_Str

To demonstrate this string LPAD function in MySQL, We are going to use the customer details table data that we shown below

MySQL String LPAD Function Example

MySQL LPAD Example 1

The MySQL String LPAD helps to pad the string expression to the left side of the original string. The following LPAD query shows multiple ways to use this string function.

SELECT LPAD('World', 11, 'Hello ');

-- It will trim the final string to length 8
SELECT LPAD('World', 8, 'Hello');

-- Let me use NULL value as input
SELECT LPAD('World', 8, NULL);

SELECT LPAD(NULL, 8, 'World');
MySQL LPAD Function Example 1

MySQL LPAD Function Example 2

In this String Function example, we are going to implement the string LPAD function on different columns in a table. The following statement adds Comp string to the Education column. It attaches dollar symbol to data present in the yearly Income column.

-- MySQL String LPAD Example
USE company;
SELECT  First_Name, 
		Last_Name,
        Education, 
        LPAD(Education, 13, 'Comp  ') AS Dollar_Income,    
        Profession, 
        Yearly_Income, 
        LPAD(Yearly_Income, 6, '
MySQL LPAD Function Example 2