Select your cookie preferences

We use essential cookies and similar tools that are necessary to provide our site and services. We use performance cookies to collect anonymous statistics, so we can understand how customers use our site and make improvements. Essential cookies cannot be deactivated, but you can choose “Customize” or “Decline” to decline performance cookies.

If you agree, AWS and approved third parties will also use cookies to provide useful site features, remember your preferences, and display relevant content, including relevant advertising. To accept or decline all non-essential cookies, choose “Accept” or “Decline.” To make more detailed choices, choose “Customize.”

LPAD and RPAD functions

Focus mode
LPAD and RPAD functions - Amazon Redshift

These functions prepend or append characters to a string, based on a specified length.

Syntax

LPAD(string1, length, [ string2 ])
RPAD(string1, length, [ string2 ])

Arguments

string1

A CHAR string, a VARCHAR string, or an expression that implicitly evaluates to a CHAR or VARCHAR type.

length

An integer that defines the length of the result of the function. The length of a string is based on the number of characters, not bytes, so that multi-byte characters are counted as single characters. If string1 is longer than the specified length, it is truncated (on the right). If length is zero or a negative number, the result of the function is an empty string.

string2

(Optional) One or more characters that are prepended or appended to string1. If this argument is not specified, spaces are used.

Return type

VARCHAR

Examples

The following examples use data from the EVENT table in the TICKIT sample database. For more information, see Sample database.

To truncate a specified set of event names to 20 characters and prepend the shorter names with spaces, use the following example.

SELECT LPAD(eventname, 20) FROM event WHERE eventid BETWEEN 1 AND 5 ORDER BY 1; +---------------------+ | lpad | +---------------------+ | Salome | | Il Trovatore | | Boris Godunov | | Gotterdammerung | |La Cenerentola (Cind | +-----------------------+

To truncate the same set of event names to 20 characters but append the shorter names with 0123456789, use the following example.

SELECT RPAD(eventname, 20,'0123456789') FROM event WHERE eventid BETWEEN 1 AND 5 ORDER BY 1; +----------------------+ | rpad | +----------------------+ | Boris Godunov0123456 | | Gotterdammerung01234 | | Il Trovatore01234567 | | La Cenerentola (Cind | | Salome01234567890123 | +----------------------+

On this page

PrivacySite termsCookie preferences
© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved.