Postgresql Lpad Function

PostgreSQL: lpad Function

In this PostgreSQL post explains how to use the PostgreSQL lpad function with syntax and examples.

Description

The PostgreSQL lpad function returns a string that is left-padded with a specified string to a certain length.

Syntax

The syntax for the lpad function in PostgreSQL is:

lpad( string, length, pad_string )

Parameters or Arguments

string

The string to left-pad.

length

The length of the result after string has been left-padded.

pad_string

The specified string to left-pad to string.

Note

  • If string is longer than length, the lpad function will remove characters from the right-hand side of string to shorten it to length characters.
  • See also the rpad function.

Applies To

The lpad function can be used in the following versions of PostgreSQL:

  • PostgreSQL 9.4, PostgreSQL 9.3, PostgreSQL 9.2, PostgreSQL 9.1, PostgreSQL 9.0, PostgreSQL 8.4

Example

Let's look at some PostgreSQL lpad function examples and explore how to use the lpad function in PostgreSQL.

For example:

(Please note that for each of the example results below, we have included single quotes around the result to demonstrate what the lpad function returns in PostgreSQL. If you ran these commands yourself, you would not see the single quotes in the result):

postgres=# SELECT lpad('AODBA.com', 18, 'A');
        lpad
----------------------

 'AAAODBA.com'
(1 row)

postgres=# SELECT lpad('AODBA.com', 19, 'A');
        lpad
-----------------------

 'AAAAODBA.com'
(1 row)

postgres=# SELECT lpad('abc', 6, ' ');
   lpad
----------

 '   abc'
(1 row)

postgres=# SELECT lpad('abc', 9, 'XYZ');
    lpad
-------------

 'XYZXYZabc'
(1 row)

postgres=# SELECT lpad('abc', 10, 'XYZ');
    lpad
--------------

 'XYZXYZXabc'
(1 row)