LPAD
Pads a string on the left with the characters of fill up to length.
PostgreSQL 17.9
LPAD(string, length[, fill])- [ ]optional part
- string
- Source stringtext
- length
- Length of the result in charactersinteger
- fill
- Fill string, repeated as needed, a space by defaulttextOptional
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.9
SELECT LPAD('9', 5, '0');Cutting off a long string
PostgreSQL 17.9
SELECT LPAD('PostgreSQL', 4, '*');Numbers with leading zeros
PostgreSQL 17.9
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.