PostgreSQL- LPAD Function
Last Updated :
18 Jul, 2024
The LPAD()
function in PostgreSQL is a powerful tool for padding a string to the left, ensuring it reaches a specified length by filling it with a designated character or characters. This function can be particularly useful in data formatting and report generation.
Let us better understand the LPAD Function in PostgreSQL from this article.
Syntax
LPAD(string, length[, fill])
Parameters:
Let's analyze the above syntax:
- string: The string to be padded on the left.
- length: A positive integer specifying the desired length of the result string after padding.
- fill: The character or characters used to pad the original string. This is an optional argument, and if not specified, the default padding character is a space.
PostgreSQL LPAD Function Examples
To better understand the LPAD()
function, let's explore a couple of examples.
Example 1: Basic Padding
The following SQL statement demonstrates how to use the LPAD()
function to pad the string 'GeeksforGeeks'
with the character '*'
on the left, ensuring the total length of the string becomes 15 characters.
SELECT LPAD('GeeksforGeeks', 15, '*');
Output:

Explanation: In this example, the original string 'GeeksforGeeks'
is padded with three asterisks ('*'
) on the left to achieve the specified length of 15 characters.
Example 2: Creating a Chart with LPAD()
The following statement illustrates how to use the LPAD() function to draw a chart based on the sum of payments per customer from the 'customer' and 'payment' table of the sample database:

SELECT first_name || ' ' || last_name fullname,
SUM(amount) total,
LPAD('*', CAST(TRUNC(SUM(amount) / 10) AS INT), '*') chart
FROM payment
INNER JOIN customer USING (customer_id)
GROUP BY customer_id
ORDER BY SUM(amount) DESC;
Output:

Explanation: In this example, the fullname
column concatenates the first and last names. The total
column sums up the payment amounts per customer. The chart
column uses LPAD()
to create a simple bar chart where the number of asterisks ('*'
) represents the sum of payments divided by 10.
Important Points About PostgreSQL LPAD Function
- If the specified length is less than the length of the original string, the
LPAD()
function will truncate the original string to fit the specified length. - If the
string
argument is an empty string, LPAD()
will return a string composed entirely of the fill
characters up to the specified length. - The
LPAD()
function can be combined with 'CAST
'
to pad numeric data after converting it to a string. - If the
fill
argument is omitted, LPAD()
defaults to using a space character for padding.
Similar Reads
PostgreSQL Tutorial In this PostgreSQL tutorial youâll learn the basic data types(Boolean, char, text, time, int etc.), Querying and Filtering techniques like select, where, in, order by, etc. managing and modifying the tables in PostgreSQL. Weâll cover all the basic to advance concepts of PostgreSQL in this tutorial.
8 min read
PostgreSQL DATEDIFF Function PostgreSQL doesnât have a DATEDIFF function like some other databases, but you can still calculate the difference between dates using simple subtraction. This approach allows you to find out how many days, months, or years separate two dates. In this article, we'll explore how to compute date differ
6 min read
PostgreSQL - Data Types PostgreSQL is a powerful, open-source relational database management system that supports a wide variety of data types. These data types are essential for defining the nature of the data stored in a database column. which allows developers to define, store, and manipulate data in a way that aligns w
5 min read
PostgreSQL - Psql commands PostgreSQL, or Postgres, is an object-relational database management system that utilizes the SQL language. PSQL is a powerful interactive terminal for working with the PostgreSQL database. It enables users to execute queries efficiently and manage databases effectively.Here, we highlight some of th
2 min read
Top 50 PostgreSQL Interview Questions and Answers Are you preparing for a PostgreSQL interview? PostgreSQL is a powerful open-source relational database management system (RDBMS) that is well-known for its reliability, scalability, and rich set of features. Itâs a favorite among developers and businesses alike, making it essential to master if we w
15+ min read
PostgreSQL - Create Database Creating a database in PostgreSQL is an important task for developers and database administrators to manage data effectively. PostgreSQL provides multiple ways to create a database, catering to different user preferences, whether through the command-line interface or using a graphical interface like
5 min read
How to Dump and Restore PostgreSQL Database? PostgreSQL remains among the most efficient and widely applied open-source relational database management systems. It provides the superior function of saving, configuring, and extracting information most effectively. In the process of migrating data, creating backups, or transferring databases betw
6 min read
PostgreSQL - SERIAL When working with PostgreSQL, we need to create tables with unique primary keys. PostgreSQL offers a powerful feature known as the SERIAL pseudo-type which simplifies generating auto-incrementing sequences for columns. In this article, weâll learn about the PostgreSQL SERIAL pseudo-type by explain h
5 min read
PostgreSQL - DISTINCT ON expression The DISTINCT ON clause in PostgreSQL allows us to retrieve unique rows based on specific columns by offering more flexibility than the standard DISTINCT clause. DISTINCT ON allow us to specify which row to keep for each unique value based on an ORDER BY clause. This is particularly useful for select
5 min read
PostgreSQL Connection String A connection string is an essential component that enables applications to communicate with databases or other data sources by providing the necessary configuration details. It consolidates critical information such as the server address, database name, user credentials, and additional parameters li
4 min read