PLSQL | LPAD Function

The PLSQL LPAD function is used for padding the left-side of a string with a specific set of characters. a prerequisite for this is that string shouldn’t be NULL. The LPAD function in PLSQL is useful for formatting the output of a query. The LPAD function accepts three parameters which are input_string, padded_length and the pad_string.

Both input_string and pad_string can be any of the datatypes CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB. The string returned is of VARCHAR2 datatype if input_string is a character datatype. The argument padded_length must be a NUMBER integer or a value that can be implicitly converted to a NUMBER integer. If you do not specify pad_string, then the default is a single blank. If input_string is longer than padded_length, then this function returns the portion of input_string that fits in padded_length.

Syntax:



LPAD( input_string, padded_length, pad_string )

Parameters Used:

input_string – It is used to specify the string which needs to be formatted.

padded_length – It is used to specify the number of characters to return. 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 – It is an optional parameter which is used to specify the input_string that will be padded to the left-hand side of string. If this parameter is omitted, the LPAD function will pad spaces to the left-side of string1.

Supported Versions of Oracle/PLSQL:

  1. Oracle 12c
  2. Oracle 11g
  3. Oracle 10g
  4. Oracle 9i
  5. Oracle 8i

Example-1:

DECLARE 
   Test_String string(20) := 'Geeksforgeeks';
   
BEGIN 
   dbms_output.put_line(LPAD(Test_String, '5')); 
   
END; 

Output:

Geeks 



Example-2:

DECLARE 
   Test_String string(20) := 'Geeksforgeeks';
   
BEGIN 
   dbms_output.put_line(LPAD(Test_String, '17')); 
   
END; 

Output:

Geeksforgeeks 



Example-3:

DECLARE 
   Test_String string(20) := 'Geeksforgeeks';
   
BEGIN 
   dbms_output.put_line(LPAD(Test_String, '17', '0')); 
   
END;  

Output:

0000Geeksforgeeks 



Example-4:

DECLARE 
   Test_String string(20) := 'Geeksforgeeks';
   
   
BEGIN 
   dbms_output.put_line(LPAD(Test_String, '12', '0')); 
   
END;   

Output:

Geeksforgeeks 


My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.