Jump to: navigation, search

PostgreSQL LPAD

From w3cyberlearnings

Contents

Postgresql lpad

This function returns left padded with a specific string. When the LENGTH is shorter than the STR length, it will return based on the LENGTH. However, when the LENGTH is longer than it will add the padstr to the left and return.

Syntax lpad

  • Str: a string
  • length: length to be padded
  • padStr: a pad string
lpad(Str,length,padStr);

Example 1

test2=# SELECT lpad('apple',4,'k');
 lpad 
------
 appl
(1 row)

test2=# SELECT lpad('apple',7,'k');
  lpad   
---------
 kkapple
(1 row)

test2=# SELECT lpad('apple',8,'k');
   lpad   
----------
 kkkapple
(1 row)

Example 2

test2=# SELECT lpad(user_firstname,15,'!'),
user_firstname
FROM profile;
      lpad       | user_firstname 
-----------------+----------------
 !!!!!!!!!!!!Jim | Jim
 !!!!!!!!!!!king | king
 !!!!!!!!!!Jammi | Jammi
 !!!!!!!!!!!!Bob | Bob
                 | 
(5 rows)

Related Links


Navigation
Web
SQL
MISC
References