LPAD
The LPAD
function returns Expression1
, left-padded to length n characters with the sequence of characters in Expression2
. This function is useful for formatting the output of a query.
SQL syntax
LPAD (Expression1, n
[,Expression2])
Parameters
LPAD
has the parameters:
Parameter | Description |
---|---|
|
|
|
Length of characters returned by the |
|
Sequence of characters to be left-padded in |
Description
-
If
Expression1
is of typeCHAR
orVARCHAR2
, the data type returned isVARCHAR2
. IfExpression1
is of typeNCHAR
orNVARCHAR2
, the data type returned isNVARCHAR2
. IfExpression1
is a LOB, the data type returned is the same as the LOB data type provided. -
The returned data type length is equal to
n
ifn
is a constant. Otherwise, the maximum result length of 8300 is returned. -
You can specify
TT_CHAR
,TT_VARCHAR
,TT_NCHAR
, andTT_NVARCHAR
forExpression1
andExpression2
. IfExpression1
is of typeTT_CHAR
orTT_VARCHAR
, the data type returned isTT_VARCHAR
. IfExpression1
is of typeTT_NCHAR
orTT_NVARCHAR
, the data type returned isTT_NVARCHAR
. -
For
CHAR
,VARCHAR2
,NCHAR
,NVARCHAR2
,CLOB
orNCLOB
data types:-
If either
Expression1
orExpression2
isNULL
, the result isNULL
. Ifn
is less than or equal to0
, the result isNULL
.
-
-
For
TT_CHAR
,TT_VARCHAR
,TT_NCHAR
andTT_NVARCHAR
types:-
If either
Expression1
orExpression2
is notNULL
and ifn
is less than or equal to 0, the result is the empty string.
-
Examples
The following prints out the last names of the first 5 employees, left-padded with periods out to 20 characters.
Command> SELECT FIRST 5 LPAD (last_name, 20, '.') FROM employees ORDER BY last_name; < ................Abel > < ................Ande > < ............Atkinson > < ..............Austin > < ................Baer > 5 rows found.
Use LPAD
function to left-pad the string 'LPAD Function'
with string 'DEMO-ONLY'
plus 2 spaces. The DEMO-ONLY
string is replicated as much as it can as defined by the total characters output by the function, which is replicated three times.
Command> SELECT LPAD ('LPAD Function', 46, 'DEMO-ONLY ') FROM dual; < DEMO-ONLY DEMO-ONLY DEMO-ONLY LPAD Function > 1 row found.
Call LPAD
function with length of -1. NULL
is returned.
Command> SELECT LPAD ('abc', -1, 'a') FROM dual; < <NULL> > 1 row found.