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 sentence. 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 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 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 in MySQL, We are going to use the customer details table data that we shown below
MySQL LPAD 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');
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, '