WELCOME, GUEST
Search in Topic Titles
Welcome to Knowledge Xpert for Oracle
Knowledge Xpert for Oracle Administration
Oracle Architecture
Database Administration
Database Tuning
Network Management
SQL Reference
Regular Expressions in Oracle
Quote Character Assignment
Built-in Packages
SQL Functions
BITAND Function
NVL2 Function
PERCENT_RANK Function
RATIO_TO_REPORT Function
REFTOHEX Function
REGR_(linear regression) functions
STDDEV_POP Function
STDDEV_SAMP Function
SYS_CONTEXT Function
SYS_GUID Function
TO_LOB Function
VAR_POP
VAR_SAMP
WIDTH_BUCKET Function
TREAT Function
CURRENT_DATE Function
CURRENT_TIMESTAMP Function
DBTIMEZONE Function
FROM_TZ Function
LOCALTIMESTAMP Function
SESSIONTIMEZONE Function
SYS_EXTRACT_UTC Function
SYSTIMESTAMP Function
TO_DSINTERVAL Function
TO_TIMESTAMP Function
TO_TIMESTAMP_TZ Function
TO_YMINTERVAL Function
TZ_OFFSET Function
EXTRACT Function
ASCIISTR Function
BIN_TO_NUM Function
CAST Function
COMPOSE Function
DECOMPOSE Function
RAWTONHEX Function
ROWIDTONCHAR Function
TO_CHAR (character) Function
TO_CHAR (number) Function
TO_CLOB Function
TO_NCHAR (character) Function
TO_NCHAR (datetime) Function
TO_NCHAR (number) Function
TO_NCLOB Function
TRANSLATE...USING Function
UNISTR Function
Single-Row Functions - COALESCE
Single-Row Functions - EXISTSNODE
Single-Row Functions - EXTRACT (XML)
Single-Row Functions - NLS_CHARSET_DECL_LEN
Single-Row Functions - NLS_CHARSET_ID
Single-Row Functions - NLS_CHARSET_NAME
Single-Row Functions - NULLIF
Single-Row Functions - SYS_CONNECT_BY_PATH
Single-Row Functions - SYS_DBURIGEN
Single-Row Functions - SYS_TYPEID
Single-Row Functions - SYS_XMLAGG
Single-Row Functions - SYS_XMLGEN
GROUP_ID Function
GROUPING_ID Function
PERCENTILE_CONT Function
PERCENTILE_DISC Function
RANK Function
ROW_NUMBER Function
DEREF Function
REF Function
VALUE Function
FIRST Function
LAST Function
CARDINALITY Function
COLLECT Function
CV Function
DEPTH Function
STATS_ONE_WAY_ANOVA Function
STATS_T_TEST_* Function
STATS_WSR_TEST Function
TO_BINARY_FLOAT Function
XMLAGG Function
XMLCOLATTVAL Function
XMLCONCAT Function
XMLELEMENT Function
XMLFOREST Function
XMLSEQUENCE Function
XMLTRANSFORM Function
EXTRACTVALUE Function
LNNVL Function
MEDIAN Function
NANVL Function
TO_BINARY_DOUBLE Function
NCHR Function
ORA_HASH Function
PATH Function
POWERMULTISET Function
POWERMULTISET_BY_CARDINALITY Function
PRESENTNNV Function
PRESENTV Function
PREVIOUS Function
REMAINDER Function
SET Function
STATS_BINOMIAL_TEST Function
STATS_CROSSTAB Function
STATS_F_TEST Function
STATS_KS_TEST Function
STATS_MODE Function
STATS_MW_TEST Function
CLUSTER_ID Function
INSERTCHILDXML Function
PREDICTION Function
INSERTXMLBEFORE Function
PREDICTION_BOUNDS Function
PREDICTION_COST Function
PREDICTION_DETAILS Function
PREDICTION_PROBABILITY Function
PREDICTION_SET Function
CLUSTER_PROBABILITY Function
CLUSTER_SET Function
CUBE_TABLE Function
DATAOBJ_TO_PARTITION Function
DELETEXML Function
FEATURE_ID Function
FEATURE_SET Function
FEATURE_VALUE Function
BFILENAME Function
EMPTY_BLOB, EMPTY_CLOB Function
ANALYZE - Examples
CORR Function
LEAD
MAKE_REF Function
NTILE Function
NUMTODSINTERVAL Function
NUMTOYMINTERVAL Function
COVAR_POP Function
COVAR_SAMP Function
CUME_DIST Function
DENSE_RANK Function
FIRST_VALUE Function
GROUPING Function
LAG
LAST_VALUE
ABS Function
CEIL Function
COS Function
COSH Function
EXP Function
FLOOR Function
LN Function
LOG Function
MOD Function
POWER Function
ROUND (number) Function
SIGN Function
SIN Function
SINH Function
SQRT Function
TAN Function
TANH Function
TRUNC Function (number)
ACOS Function
ASIN Function
ATAN Function
ATAN2 Function
AVG Function
COUNT Function
MAX Function
MIN Function
STDDEV Function
SUM Function
VARIANCE Function
ASCII Function
CHR Function
CONCAT Function
INITCAP Function
INSTR Function
LENGTH Function
LOWER Function
LPAD Function
LTRIM Function
NLS_INITCAP Function
NLS_LOWER Function
NLS_UPPER Function
NLSSORT Function
REPLACE Function
RPAD Function
RTRIM Function
SOUNDEX Function
SUBSTR Function
TRANSLATE Function
UPPER Function
CHARTOROWID Function
CONVERT Function
HEXTORAW Function
RAWTOHEX Function
ROWIDTOCHAR Function
TO_CHAR (date conversion) Function
TO_DATE Function
TO_MULTI_BYTE Function
TO_NUMBER Function
TO_SINGLE_BYTE Function
ADD_MONTHS Function
LAST_DAY Function
MONTHS_BETWEEN Function
NEW_TIME Function
NEXT_DAY Function
ROUND (date) Function
NVL Function
UID Function
USER Function
USERENV Function
VSIZE Function
SQLCODE Function Syntax
SQLERRM Function Syntax
TRIM Function
DECODE FunctionFunction
DUMP Function
GREATEST Function
LEAST Function
Analytic Functions
Character Functions
Collection Functions
Conversion Functions
Datetime Functions
Miscellaneous Functions
Model Functions
Numeric Functions
Object Reference Functions
SYSDATE Function
GREATEST_LB Function
LEAST_UB Function
SQL *Plus
SQL Statements
Keyword Reserved Words
ANSI Reserved Words
SQL Reserved Words
SQL Coding Best Practices
Instant Scripts
Disclaimer
Knowledge Xpert for PL/SQL Development
Knowledge Xpert Feedback

LPAD Function

The LPAD or Left Pad function returns char1, left-padded to length n with the sequence of characters in char2; char2 defaults to a single blank. If char1 is longer than n, this function returns the portion of char1 that fits in n.

Both char1 and char2 can be any of the datatypes CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB. The string returned is of VARCHAR2 datatype and is in the same character set as char1.

The argument n is the total length of the return value as it is displayed on your terminal screen. In most character sets, this is also the number of characters in the return value. However, in some multibyte character sets, the display length of a character string can differ from the number of characters in the string.

Syntax

>──LPAD──(char1 , n ─┬─────────┬─)──><
                     └─, char2─┘

PL/SQL Example

Display the number padded left with zeros to a length of 10:

LPAD ('55', 10, '0') ==> '0000000055'

Display the number padded left with zeros to a length of 5:

LPAD ('12345678', 5, '0') ==> '12345'

LPAD interprets its padded_length as the maximum length of the string that it may return. As a result, it counts padded_length number of characters from the left (start of the string) and then simply returns that substring of the incoming value.

Place the phrase "sell!" in front of the names of selected stocks, up to a string length of 45:

LPAD ('HITOP TIES', 45, 'sell!') 
==>
   'sell!sell!sell!sell!sell!sell!sell!HITOP TIES'

Because the length of "HITOP TIES" is 10 and the length of "sell!" is 5, there is room for seven repetitions of the pad string. LPAD does, in fact, generate a repetition of the pattern specified in the pad string.

Place the phrase "sell!" in front of the names of selected stocks, up to a string length of 43.

LPAD ('HITOP TIES', 43, 'sell!') 
==>
   'sell!sell!sell!sell!sell!sell!selHITOP TIES'

Because the length of "HITOP TIES" is 10 and the length of "sell!" is 5, there is no longer room for seven full repetitions of the pad string. As a result, the seventh repetition (counting from the left) of "sell!" lost its last two characters. So you can see that LPAD does not pad by adding to the left of the original string until it runs out of room. Instead, it figures out how many characters it must pad by to reach the total, then constructs that full padded fragment, and finally appends the original string to this fragment.

Place three repetitions of the string "DRAFT-ONLY" in front of the article's title. Put two spaces between each repetition.

LPAD ('Why I Love PL/SQL', 53, 'DRAFT-ONLY  ');
==>
      'DRAFT-ONLY  DRAFT-ONLY  DRAFT-ONLY  Why I Love PL/SQL'

You can specify any number of characters to be padded in front of the incoming string value. The 53 that is hardcoded in that call to LPAD is the result of some hard calculations: the title is 17 characters and the prefix, including spaces, is 12 characters. As a result, you need to make sure that you pad to a length of at least 17 + 12*3 = 53. This is not a very desirable way to solve the problem, however; once again we have assumed that in the program we have access to all these specific values and can precompute the length we need.

SQL Example

The following example left-pads a string with the characters "*.":

SELECT LPAD('Page 1',15,'*.') "LPAD example"
     FROM DUAL;

LPAD example
---------------
*.*.*.*.*Page 1

Syntax diagrams and parameter descriptions adapted from Oracle, Inc. documentation.
Rating (Votes: 0)

Note: Only Registered Users may rate topics.