Name
LPAD
Synopsis
By default, PL/SQL strips all trailing blanks from a character string unless it is declared with a fixed-length CHAR datatype. There are occasions, however, when you really want some spaces (or even some other characters) added to the front or back of your string. LPAD (and its right-leaning cousin, RPAD) give you this capability. The LPAD function returns a string padded to the left (hence the “L”) to a specified length and with a specified pad string. The specification of the LPAD function is:
FUNCTION LPAD (string1IN VARCHAR2,padded_lengthIN NUMBER [,pad_stringIN VARCHAR2]) RETURN VARCHAR2
LPAD returns string1 padded on the left to
length padded_length with the optional character
string pad_string. If you do not specify
pad_string, then string1 is
padded on the left with spaces. You must specify the
padded_length. If string1
already has a length equal to padded_length,
then LPAD returns string1 without any additional
characters. If padded_length is smaller than the
length of string1, LPAD effectively truncates
string1—it returns only the first
padded_length characters of the incoming
string1.
As you can see, LPAD can do a lot more than just add spaces to the left of a string. Let’s look at some examples of how useful LPAD can be.
Display the number padded left with zeros to a length of 10:
LPAD ('55', 10, '0') --> '0000000055'Display the number padded left with zeros to a length of 5:
LPAD ('12345678', 5, '0') --> '12345'LPAD interprets its
padded_length ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access