Function Reference

LPAD

Pads a string on the left with the characters of fill up to length.

PostgreSQL 17.5
LPAD(string, length[, fill])

Parameters

stringtext
Source string
lengthinteger
Length of the result in characters
filltextoptional
Fill string, repeated as needed, a space by default

Return value

A string of exactly length characters: a longer string is cut off on the right; with an empty fill the string is not padded

Examples

Padding with zeros on the left

PostgreSQL 17.5
SELECT LPAD('9', 5, '0');
lpad
00009

Cutting off a long string

PostgreSQL 17.5
SELECT LPAD('PostgreSQL', 4, '*');
lpad
Post

Numbers with leading zeros

PostgreSQL 17.5
SELECT LPAD(member_id::text, 3, '0') AS code,
	member_name
FROM FamilyMembers;
codemember_name
001Headley Quincey
002Flavia Quincey
003Andie Quincey
004Lela Quincey
005Annie Quincey
006Ernest Forrest
007Constance Forrest
008Wednesday Addams

Details

The first argument must be a string: cast a number to text first, for example LPAD(member_id::text, 3, '0'), while LPAD(5, 3, '0') causes an error.

See also