lpad(text, integer, text) — Pad a string on the left

Last updated: 2026-11-04

lpad(text, integer, text) — Pad a string on the left

lpad() pads a string on the left with a fill character (a space by default) until it reaches the requested length, and truncates from the right when the string is already longer. Its typical jobs are producing fixed-width codes and zero-padding numbers, for example turning an order number into a uniform 8-character identifier.

Kindfunction
Example LanguageSQL

📝 Syntax

lpad(string text, length integer [, fill text]) → text
rpad(string text, length integer [, fill text]) → text

⚙️ Parameters

string textThe string to pad. Type text.
length integerThe target total length in characters. Type integer.
fill textOptional: the fill character, a space by default; it may be several characters long and is repeated as needed. Type text.

Returns:text — the padded (or truncated) string.

▶ Example

Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.

Output:

 id | order_no | padded_code
----+----------+-------------
  1 |       42 | 00000042
  2 |     1234 | 00001234
  3 |   987654 | 00987654
(3 rows)

📌 Key Points

More case studies coming soon.

📚 Related Reference

❓ FAQ

QCan lpad() overflow if the string grows too long?
ANo. The second argument is the final length: a longer input is truncated on the right, a shorter one is padded, so the result length always equals that argument.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏