How to use LPAD and RPAD Function

If you want to pad any character on the left side or right side of a string, you can use LPAD and RPAD functions. In this article you will see how to use LPAD and RPAD functions in Oracle.

LPAD and RPAD function

LPAD function:

The LPAD function is used to pad the left side of a string with a specific set of characters.

LPAD Syntax:

The syntax for the LPAD function is:

LPAD( string_to_pad, padded_length, [ pad_string ] )

String_to_pad is the string to pad characters to the left hand side.

padded_length is the number of characters to pad in the string. If the padded_length is smaller than the original string, the LPAD function will truncate the string to the size of padded_length.

pad_string is the optional field. This is the string that will be padded to the left hand side of String_to_pad. If this parameter is absent, the LPAD function will pad spaces to the left side of String_to_pad.

Examples:

The LPAD function can be used in Oracle SQL and PLSQL:

SQL> select LPAD('ITBloggerTips',15) from dual;

LPAD('ITBLOGGER

---------------

'  ITBloggerTips'

SQL> select LPAD('ITBloggerTips',9) from dual;

LPAD('ITB

---------

ITBlogger

SQL> select LPAD('ITBloggerTips',15,'0') from dual;

LPAD('ITBLOGGER

---------------

00ITBloggerTips

SQL> select LPAD('ITBloggerTips',20,'A') from dual;

LPAD('ITBLOGGERTIPS'

--------------------

AAAAAAAITBloggerTips

SQL> select LPAD('ITBloggerTips',9,'A') from dual;

LPAD('ITB

---------

ITBlogger

SQL> select LPAD('ITBloggerTips',13,'A') from dual;

LPAD('ITBLOGG

-------------

ITBloggerTips

SQL>

RPAD Function:

The RPAD function is used to pad the right side of a string with a specific set of characters.

RPAD Syntax:

The syntax for the RPAD function is:

RPAD(string_to_pad, padded_length, [ pad_string ])

String_to_pad is the string to pad characters to the right hand side.

padded_length is the number of characters to pad in the string. If the padded_length is smaller than the original string, the RPAD function will truncate the string to the size of padded_length.

pad_string is the optional field. This is the string that will be padded to the right hand side of String_to_pad. If this parameter is absent, the RPAD function will pad spaces to the right side of String_to_pad.

Examples:

The RPAD function can be used in Oracle SQL and PLSQL:

SQL> select RPAD('ITBloggerTips',15) from dual;

RPAD('ITBLOGGER

---------------

ITBloggerTips

SQL> select RPAD('ITBloggerTips',9) from dual;

RPAD('ITB

---------

ITBlogger

SQL> select RPAD('ITBloggerTips',15,'0') from dual;

RPAD('ITBLOGGER

---------------

ITBloggerTips00

SQL> select RPAD('ITBloggerTips',20,'A') from dual;

RPAD('ITBLOGGERTIPS'

--------------------

ITBloggerTipsAAAAAAA

SQL> select RPAD('ITBloggerTips',9,'A') from dual;

RPAD('ITB

---------

ITBlogger

SQL> select RPAD('ITBloggerTips',13,'A') from dual;

RPAD('ITBLOGG

-------------

ITBloggerTips

SQL>

If you know more about it, write below in comment section.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
Scroll to Top