Open In App

PostgreSQL- LPAD Function

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:



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:

Article Tags :