MySQL LPAD Function

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

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 one that is available in a column.
  • length: This is the length of a final sentence (after padding). If the final one is greater than this length value, then extra characters are trimmed. Remember, You can use the Length function to calculate the MySQL length.
  • Pad_String: String that you want to add to the left side of the Original_Str

To demonstrate this string LPAD function, we will use the customer details table data shown below.

Customer Table

MySQL LPAD Function Example

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

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

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

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

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

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

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