w3resource

Oracle LPAD function

Description

The Oracle LPAD() function is used to padding the left side of a string with a specific set of characters. The function is useful for formatting the output of a query.

Syntax:

LPAD(expr1, n [, expr2 ])

Parameters:

Name Description Data Types
expr1 Original string. CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB
n A number indicating the total length of the string ( in characters) returned after padding. NUMBER integer or a value that can be implicitly converted to a NUMBER integer.
expr2 String which is used for left padding. CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB

Return Value

CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB

Applies to

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

Pictorial Presentation

Oracle LPAD function pictorial presentation

Example: Oracle LPAD() function

In the following Oracle statement, the first argument specifies a string of 6 characters, the second argument specifies that the length of the string returned after padding will be 10 characters and the third argument specifies the string to be used for left padding. So, 4 characters (10-6) being used for left padding and the function thus returns '++++Oracle'.

SQL> SELECT LPAD('Oracle',10,'+') FROM DUAL;

Sample Output:

LPAD('ORAC
----------
++++Oracle

Example: Oracle of LPAD() function with less than the original string

The following Oracle statement returns 'Orac'. This happens because, the first argument has 6 characters, second argument 4 is the total number of characters after left padding and the third argument is the padding string. Since a total number of characters after padding is less than the total number of characters in the first argument, so to meet the condition, two characters are omitted from the actual string (i.e. the first argument).

SQL> SELECT LPAD('Oracle',4,'*') FROM DUAL;
 

Sample Output:

LPAD
----
Orac

Previous: LOWER
Next: LTRIM



Share this Tutorial / Exercise on : Facebook and Twitter