The second parameter (n) to lpad or rpad determines the resulting length. If it is longer than n, string will be truncated:
select
lpad('string with 25 characters', 20) lpad,
rpad('string with 25 characters', 20) rpad
from dual;
--
-- LPAD RPAD
-- -------------------- --------------------
-- string with 25 chara string with 25 chara
The literal string is lpadded to all lengths between 1 and 15. The < shows where the string is cut. As soon as the second parameter becomes larger than the length of the first parameter, lpadding occurs. The default is space, so spaces are prepended to string:
begin
for i in 1 .. 15 loop
dbms_output.put_line(
lpad('string', i) || '<'
);
end loop;
end;
/
--
-- s<
-- st<
-- str<
-- stri<
-- strin<
-- string<
-- string<
-- string<
-- string<
-- string<
-- string<
-- string<
-- string<
-- string<
-- string<
Same thing as above, but with rpad instead of lpad:
The maximum length returned by rpad or lpad is determined by the value of max_string_size. If the value is standard, this length is 4000 otherwise 32767.
The following query returns 4000 (or 32767), rather than throwing an error:
select
length(rpad('*', 1234567890, '*')) len
from
dual;
Side effects of empty strings
Oracle treats an empty string as null. And nulls are always a bit special within Oracle. Particularly, rpad('', n) and lpad('', n) return null.