Home Functions LPAD()
SQL String Function

LPAD()

LPAD() left pads a string with a fill string until it reaches a target length, which makes it ideal for zero padding numbers and aligning fixed width output.

MySQLPostgreSQLSQLite
Returns: A string padded on the left to the target length. If the input is already longer than the target length it is truncated to that length.

Syntax

LPAD(string, length)LPAD(string, length, pad_string)
ParameterTypeRequiredDescription
string string or expression yes The value to pad. Numbers are usually cast to text first so the result is a fixed width string.
length integer yes The total length of the result. If the string is shorter it is filled on the left; if it is longer it is truncated to this many characters.
pad_string string no The fill text repeated on the left until the target length is reached. Defaults to a single space in most engines. Commonly a zero for invoice ids.

How it works

LPAD() stands for left pad. It takes a value, a target length, and a fill string, then repeats the fill on the left side of the value until the result is exactly the target length. Its mirror image is RPAD(), which fills on the right instead.

The most common use is turning a plain number into a fixed width code. An invoice id of 42 becomes 000042 when you pad it to six characters with zeros, so every id sorts and lines up neatly. LPAD is also used to align text columns in reports and to build fixed layout strings such as account numbers or reference codes.

Because LPAD() works on text, numeric inputs are normally passed through a string conversion such as CAST first. Keep in mind that padding is a display concern: it is usually cheaper and cleaner to store the raw number and pad only when you present it, rather than storing the padded text.

One important gotcha: SQL Server has no LPAD() function. You emulate it with RIGHT(REPLICATE(pad, n) + value, n), which prepends enough copies of the pad and then keeps the rightmost n characters.

Examples

Zero pad an invoice id to six digits

SELECT LPAD(CAST(id AS CHAR), 6, '0') AS invoice_no
FROM invoices;
Result
invoice_no
==========
000042
000107
013500

Pad with spaces to align a column

SELECT LPAD(name, 10, ' ') AS aligned
FROM products;
Result
aligned
==========
     Apple
    Banana
      Mango

Build a fixed width badge code

SELECT LPAD(dept_code, 8, '*') AS badge
FROM staff;
Result
badge
========
***SALES
*FINANCE
******IT

Emulate LPAD in SQL Server (no native LPAD)

/* SQL Server has no LPAD, so prepend zeros then keep the last 6 chars */
SELECT RIGHT(REPLICATE('0', 6) + CAST(id AS VARCHAR(6)), 6) AS invoice_no
FROM invoices;
Result
invoice_no
==========
000042
000107
013500

Common mistakes

Wrong
/* The number is already 9 digits, longer than 6.
   LPAD does NOT leave it alone, it TRUNCATES it. */
SELECT LPAD('123456789', 6, '0');
/* returns 123456, the account number is now wrong */
Right
/* Pick a length that fits your widest value, or
   validate the input length before padding */
SELECT LPAD('123456789', 9, '0');
/* returns 123456789 */

The single biggest LPAD surprise: when the input is longer than the target length it is truncated from the right, not returned unchanged. Always size the length for your largest expected value.

Wrong
/* Runs on MySQL, PostgreSQL and SQLite,
   but FAILS on SQL Server: no such function */
SELECT LPAD(CAST(id AS CHAR), 6, '0') FROM invoices;
Right
/* Portable SQL Server version */
SELECT RIGHT(REPLICATE('0', 6) + CAST(id AS VARCHAR(6)), 6)
FROM invoices;

SQL Server does not implement LPAD(). If your query must run there, use RIGHT(REPLICATE(pad, n) + value, n) or the FORMAT function for numbers.

Wrong
/* Passing a raw number can behave oddly or error,
   because LPAD expects text, not an integer */
SELECT LPAD(id, 6, '0') FROM invoices;
Right
/* Cast the number to text first */
SELECT LPAD(CAST(id AS CHAR), 6, '0') FROM invoices;

LPAD is a string function. Convert numeric columns with CAST so the engine treats the value as text and the padding count is measured in characters.

Performance

LPAD() itself is very cheap: it builds one short string per row. The real cost comes from where you apply it. Padding is a formatting step, so run it in the final SELECT that feeds your report, not deep inside a subquery that processes millions of intermediate rows.

Never pad a column inside a WHERE or JOIN predicate on a large table, for example WHERE LPAD(code, 8, '0') = '00001234'. Wrapping the column in a function hides it from any index, forcing a full scan. Pad the constant on the other side of the comparison instead, or store a canonical value.

When you only need padded output for humans, consider padding in the application layer after the rows come back. That keeps the database doing set work and moves cheap string formatting to the client. For more text helpers see the full SQL functions reference and the SQL functions list.

Interview questions

What does the LPAD function do?

LPAD(string, length, pad) returns the string padded on the left with copies of the pad string until it reaches the target length. It is used for zero padding numbers and aligning fixed width columns.

What happens if the input string is longer than the target length?

It is truncated, not left alone. LPAD('123456789', 6, '0') returns 123456. This is the most common LPAD bug, so always size the length for your widest value.

How do you zero pad an integer id to a fixed width?

Cast the number to text and pad with zeros, for example LPAD(CAST(id AS CHAR), 6, '0'). The cast is needed because LPAD is a string function.

SELECT LPAD(CAST(id AS CHAR), 6, '0') AS invoice_no
FROM invoices;

How do you left pad a string in SQL Server, which has no LPAD?

Prepend enough pad characters with REPLICATE, then keep the rightmost n characters with RIGHT: RIGHT(REPLICATE('0', 6) + CAST(id AS VARCHAR(6)), 6).

SELECT RIGHT(REPLICATE('0', 6) + CAST(id AS VARCHAR(6)), 6) AS invoice_no
FROM invoices;

What is the difference between LPAD and RPAD?

RPAD() fills on the right side of the value, which is handy for aligning text to the left edge, while LPAD fills on the left, which is what you want for numbers and right aligned columns.

Master SQL, one function at a time

Browse the full SQL functions library, or learn the fundamentals with our free, structured courses.