Beginner

319. LPAD Function

Learn how to add characters to the beginning of a string until it reaches a specified length.

📘 Lesson 319 of 728 ⏱ 9 min 🎯 Intermediate

LPAD pads the beginning of a string with a specified character or sequence until the requested length is reached.

Standard-length identifiers are often required for reporting, file exports, and system integrations.

Add leading zeros to an employee ID.

SQL
SELECT
    EmployeeID,
    LPAD(CAST(EmployeeID AS VARCHAR), 6, '0') AS FormattedEmployeeID
FROM Employees;

LPAD is useful for formatting account numbers, employee IDs, product codes, and fixed-width export fields.

  • LPAD adds characters to the beginning.
  • A target length is specified.
  • It is useful for fixed-length identifiers.
  • Leading zeros are a common use case.
  • Function availability varies by database.
What does LPAD do?

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

Why use LPAD with employee IDs?

LPAD can create consistently formatted identifiers such as 000123 or 001245.

Have you finished this lesson?

Introduction to SQL – All Lessons