PostgreSQL LPAD() Function
Summary: in this tutorial, you will learn how to use the PostgreSQL LPAD()
function to pad a string on the left to a specified length with a sequence of characters.
Introduction to the PostgreSQL LPAD() function
The LPAD() function pad a string on the left to a specified length with a sequence of characters.
Here’s the basic syntax of the LPAD()
function:
The LPAD()
function accepts 3 arguments:
1) string
is a string that should be padded on the left
2) length
is an positive integer that specifies the length of the result string after padding.
Note that if the string is longer than the length argument, the string will be truncated on the right.
3) fill
is a string used for padding.
The fill
argument is optional. If you omit the fill
argument, its default value is a space.
The PostgreSQL LPAD()
function returns a string left-padded to length
characters.
PostgreSQL LPAD() function Examples
Let’s see some examples of using the LPAD()
function.
1) Basic PostgreSQL LPAD() function example
The following statement uses the LPAD()
function to pad the ‘*’ on the left of the string ‘PostgreSQL’:
The result is:
In this example, the length of the PostgreSQL
string is 10, and the result string should have a length of 15. Therefore, the LPAD()
function pads 5 asterisks (*) on the left of the string.
2) Padding leading zeros
The following example uses the LPAD()
function to pad zeros at the beginning of the string to a length of five characters:
Output:
If you want to pad a number, you need to convert that number to a string before padding. For example:
Output:
3) Using LPAD() function with table data
See the following customer
and payment
tables from the sample database:
The following statement illustrates how to use the LPAD()
function to draw a chart based on the sum of payments per customer.
The following picture illustrates the result:
In this example,
- First, add up the payments for each customer using the
SUM()
function and theGROUP BY
clause, - Second, calculate the length of the bar chart based on the sums of payments using various functions:
TRUNC()
to truncate the total payments,CAST()
to convert the result of theTRUNC()
to an integer. To make the bar chart more readable, we divided the sum of payments by 10. - Third, apply the
LPAD()
function to pad the character (*) based on the result of the second step above.
Summary
- Use the PostgreSQL
LPAD()
function to pad characters on the left of a string to a certain length.