Skip to content

LPAD()

Function Description

The function LPAD(str,len,padstr) is filled with padstr on the left side of the string str until the string with a total length of len, and finally returns the filled string. If the length of str is greater than len, the last length will be reduced to len.
If len is a negative number, NULL is returned.

Function Syntax

> LPAD(str,len,padstr)

Parameter definition

Parameters Description
str Required parameters, filled string. Both CHAR and VARCHAR types are available.
len Required parameters, total length to be filled.
padstr Required parameter, used for filling strings. Both CHAR and VARCHAR types are available.

Example

> drop table if exists t1;
> CREATE TABLE t1(Student_id INT,Student_name VARCHAR(100),Student_Class CHAR(20));
> INSERT INTO t1
VALUES
(1,'Ananya Majumdar', 'IX'),
(2,'Anushka Samanta', 'X'),
(3,'Aniket Sharma', 'XI'),
(4,'Anik Das', 'X'),
(5,'Riya Jain', 'IX'),
(6,'Tapan Samanta', 'X');
> SELECT Student_id, Student_name,LPAD(Student_Class, 10, ' _') AS LeftPaddedString FROM t1;
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| Student_id | Student_name | LeftPaddedString |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 1 | Ananya Majumdar | _ _ _ _ IX |
| 2 | Anushka Samanta | _ _ _ _ X |
| 3 | Aniket Sharma | _ _ _ _ XI |
| 4 | Anik Das | _ _ _ _ X |
| 5 | Riya Jain | _ _ _ _ IX |
| 6 | Tapan Samanta | _ _ _ _ X |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> SELECT Student_id, lpad(Student_name,4,'new') AS LeftPaddedString FROM t1;
+----------------------------------------+
| Student_id | LeftPaddedString |
+----------------------------------------+
| 1 | Anan |
| 2 | Anus |
| 3 | Anik |
| 4 | Anik |
| 5 | Riya |
| 6 | Tapa |
+----------------------------------------+
> SELECT Student_id, lpad(Student_name,-4,'new') AS LeftPaddedString FROM t1;
+----------------------------------------+
| Student_id | LeftPaddedString |
+----------------------------------------+
| 1 | NULL |
| 2 | NULL |
| 3 | NULL |
| 4 | NULL |
| 5 | NULL |
| 6 | NULL |
+----------------------------------------+
> SELECT Student_id, lpad(Student_name,0,'new') AS LeftPaddedString FROM t1;
+----------------------------------------+
| Student_id | LeftPaddedString |
+----------------------------------------+
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
+----------------------------------------+