LPAD()

SQL · String Functions

Theory

LPAD(string, length, pad) adds the pad character(s) to the left of a string until it reaches the target length. LPAD('7', 3, '0') returns '007' and LPAD('42', 5, '0') returns '00042'. If the string is already the target length, it comes back unchanged. If it is longer, LPAD cuts it down to length, keeping the left part, so LPAD('123456', 3, '0') gives '123', which can silently damage data.

LPAD is available in MySQL, Postgres, Oracle and Snowflake, but not in SQL Server or SQLite. In SQL Server the usual trick is RIGHT('00000' + CAST(id AS VARCHAR), 5). In SQLite you can write SUBSTR('00000' || id, -5) or use printf('%05d', id). These tricks keep the right part when the value is too long, unlike LPAD. The classic use is fixed-width IDs: invoice numbers like INV-00042, employee codes and file names that should sort correctly as text.

Why we use it

Numbers stored or shown as text sort badly ('10' comes before '9') and look inconsistent. Zero-padding to a fixed width fixes sorting and gives the neat codes that invoices, reports and file names need.

When to use it

Use LPAD for zero-padded codes and right-aligned fixed-width text. Make sure the length is at least as long as your biggest value, or data gets cut. If you only need formatting for display, a number format function may be simpler.

How it works

  1. Take the string, target length and pad character
  2. Is the string longer than the target? Yes: Cut it to the target length. No: Continue.
  3. Count how many characters are missing
  4. Repeat the pad on the left to fill the gap
  5. Return the fixed-width string

Syntax

LPAD(string, length, pad_char)

Example

SELECT LPAD('7', 3, '0');
-- '007'

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic