Beginner

238. LPAD Function

Learn how to add characters to the left side of a string until it reaches a desired length.

📘 Lesson 238 of 728 ⏱ 9 min 🎯 Intermediate

The LPAD function pads a string on the left with specified characters until the resulting string reaches a requested length.

Padding is useful when business identifiers must follow a fixed-width format.

Convert employee ID 25 into a five-character value such as 00025.

SQL
SELECT
    LPAD(CAST(EmployeeID AS VARCHAR(5)), 5, '0') AS FormattedID
FROM Employees;

LPAD is useful for fixed-length identifiers, account numbers, reporting formats, and data exports.

  • LPAD adds characters to the left.
  • A target length is specified.
  • A padding character can be provided.
  • It is useful for fixed-width values.
  • Support varies between database systems.
What does LPAD do?

LPAD adds characters to the left of a string until it reaches the specified length.

Why use LPAD for IDs?

It can create consistent fixed-length identifiers such as 00025 or 00150.

Have you finished this lesson?

Introduction to SQL – All Lessons