Open In App

PostgreSQL- LPAD Function

Last Updated : 01 Feb, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The PostgreSQL LPAD() function is used for padding a string to the left by a specified length with other specified characters.

Syntax: LPAD(string, length[, fill])

Let’s analyze the above syntax:

  • The string argument is a string that is to be padded on the left.
  • It is a positive integer that sets the length of the result string after padding.
  • The fill argument is used to pad the original string. It is an optional argument.By default, its value is a space.

Example 1:

The following statement uses the LPAD() function to pad the ‘*’ on the left of the string ‘GeeksforGeeks’:

SELECT LPAD('GeeksforGeeks', 15, '*');

Output:

Example 2:

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:


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads