lpad()

A system function for padding the left side of a string

lpad() is a system function for expanding a string by expanding the left side with one or more occurrences of a character or string (by default a space).

lpad() was added in PostgreSQL 6.1.

Usage

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

string will be expanded to length characters by padding the left side with a space, or if specified with fill, a character or string. If more than one character is specified in fill, the string will be repeated in the padding, and truncated if necessary.

If string is already longer than length, then string will be truncated (on the right-hand side) to length.

Change history

Examples

Basic usage of lpad():

postgres=# SELECT lpad('foo', 6);
  lpad  
--------
    foo
(1 row)

Using a non-default padding character:

postgres=# SELECT lpad('foo', 6, '!');
  lpad  
--------
 !!!foo
(1 row)

Using a padding string:

postgres=# SELECT lpad('foo', 8, '!?');
   lpad   
----------
 !?!?!foo
(1 row)

Attempting to pad a string which is longer than the specified padding length:

postgres=# SELECT lpad('foobar', 3);
 lpad 
------
 foo
(1 row)

Attempting to pad a string to a negative length:

Time: 1.039 ms
postgres=# SELECT lpad('foobar', -3);
 lpad 
------
 
(1 row)

Categories

System function

See also

rpad()