w3resource

MySQL LPAD() function

LPAD() function

MySQL LPAD() left pads a string with another string. The actual string, a number indicating the length of the padding in characters (optional) and the string to be used for left padding - all are passed as arguments.

This function is useful in -

  • String padding: It allows you to left-pad a string with a specific character or set. This is useful for aligning text or numbers in fixed-width formats.
  • Data presentation: LPAD() can format or present data in a desired format. We can use it to align text or display numbers with leading zeros.
  • String manipulation: LPAD() is often used to construct dynamic strings or generate values in specific formats.

Syntax:

LPAD(str, len, padstr)

Arguments:

Name Description
str Actual string.
len A number indicating the total length of the string ( in characters) returned after padding.
padstr String which is used for left padding.

MySQL Version: 8.0

Pictorial Presentation:

MySQL LPAD() pictorial presentation

Example : MySQL LPAD() function

In the following MySQL statement, the first argument specifies a string of 6 characters, the second argument specifies that the length of the string returned after padding will be 10 characters and the third argument specifies the string to be used for left padding. So, 4 characters (10-6) are used for left padding and the function thus returns '*****Hello.

Code:

SELECT LPAD('Hello',10,'**');

Output:

mysql> SELECT LPAD('Hello',10,'**');
+------------------------+
| LPAD('Hello',10,'**') |
+------------------------+
| *****Hello             | 
+------------------------+
1 row in set (0.03 sec)

Example of LPAD() function with less than the original string

The following MySQL statement returns 'Hell'. This happens because, the first argument has 6 characters, second argument 4 is the total number of characters after left padding and the third argument is the padding string. Since a total number of characters after padding is less than the total number of characters in the first argument, so to meet the condition, two characters are omitted from the actual string (i.e. the first argument).

Cdoe:

SELECT LPAD('Hello',4,'**');

Output:

mysql> SELECT LPAD('Hello',4,'**');
+-----------------------+
| LPAD('Hello',4,'**')  |
+-----------------------+
| Hell                  | 
+-----------------------+
1 row in set (0.00 sec)

Example of LPAD() function using column of a table

The following MySQL statement returns all the rows from publisher table, with the value of column country left padded with the string ‘**’, while after padding, the length of the string returned will be a value obtained by subtracting the length of the country () from 25.

Code:

SELECT LPAD(country,25-(length(country)),'**')
FROM publisher;

Sample table: publisher

Output:

mysql> SELECT LPAD(country,25-(length(country)),'**')
    -> FROM publisher;
+-----------------------------------------+
| LPAD(country,25-(length(country)),'**') |
+-----------------------------------------+
| *******************USA                  | 
| ***************India                    | 
| *******Australia                        | 
| *********************UK                 | 
| *******************USA                  | 
| *******************USA                  | 
| *********************UK                 | 
| ***************India                    | 
+-----------------------------------------+
8 rows in set (0.00 sec)

Example of LPAD() function with where clause

The following MySQL statement returns all the rows from publisher table who is having the country name beginning with the letter ‘U’ and column country left padded with the string ‘**’. After padding, the length of the string will be the subtraction of field size and the length of the content of the field.

Code:

SELECT LPAD(country,25-(length(country)),'**')
FROM publisher
WHERE LEFT(country,1)=’U’;

Sample table: publisher

Output:

mysql> SELECT LPAD(country,25-(length(country)),'**')
    -> FROM publisher
    -> WHERE LEFT(country,1)='U'; 
+-----------------------------------------+
| LPAD(country,25-(length(country)),'**') |
+-----------------------------------------+
| *******************USA                  | 
| *********************UK                 | 
| *******************USA                  | 
| *******************USA                  | 
| *********************UK                 | 
+-----------------------------------------+
5 rows in set (0.00 sec)

Video Presentation:

All String Functions (Slides presentation)

Previous: LOWER
Next: LTRIM



Follow us on Facebook and Twitter for latest update.