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');Cutting off a long string
PostgreSQL 17.5
SELECT LPAD('PostgreSQL', 4, '*');Numbers with leading zeros
PostgreSQL 17.5
SELECT LPAD(member_id::text, 3, '0') AS code,
member_name
FROM FamilyMembers;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.