Name

LPAD()

Synopsis

LPAD(string, length, padding)

This function adds padding to the left end of string, stopping if the combination of string and the added padding reach length characters. If length is shorter than the length of the string, the string will be shortened starting from the left to comply with the length constraint. The padding can be any character. Here is an example:

SELECT LPAD(course_name, 25, '.') AS Courses
FROM courses
WHERE course_code LIKE 'ENGL%'
LIMIT 3;

+---------------------------+
| Courses                   |
+---------------------------+
| .........Creative Writing |
| .....Professional Writing |
| ......American Literature |
+---------------------------+

In this example, a list of three courses is retrieved and the results are padded with dots to the left of the course names.

Get MySQL in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.