PostgreSQL – LPAD Function

The LPAD function is used to pad a string on the left side with a specified character or a repeated sequence of characters, until the resulting string reaches a specified length. This function is often used to align strings to a specific width.

The syntax of the LPAD function is as follows:

LPAD(string, length, fill_character)

where string is the input string that you want to pad,

and length is the desired length of the resulting padded string,

and fill_character is the character or sequence of characters used to pad the input string.

Here’s an example of how you might use the LPAD function in a SQL query:

SELECT LPAD('Hello', 10, '*') AS padded_string;

In this example, the result would be the string '*****Hello', as the LPAD function added 5 asterisks (*) to the left side of the input string 'Hello' to make it a total of 10 characters long.

You can also pad with spaces or other characters:

SELECT LPAD('123', 5, '0') AS padded_number;

In this case, the result would be the string '00123', as the LPAD function added two 0 characters to the left side of the input string '123'.

The LPAD function can be useful when you need to format and align data in your SQL queries or reports.

Similar Posts